如何把DataGridView中的多维表头生成到Excel 如何把DataGridView中的多维表头生成到Excel表头,格式不变 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 如何导入数据至Excel文件 C#.net 多维表头可以自己操作单元格,写出来。或者你读取excel模板,往模板插入数据也可以。 using (MemoryStream m = bll.ExportQualifiedExaminees(Server.MapPath("~/Resources/考生签到表导出模版.xls"), list1[0].fServiceName, list, Server.MapPath("~/Common/Images/toeic_log.PNG"))) { ExcelExportHandler.ExportFile(m, "application/ms-excel", "UTF-8", "GB2312", "考生签到表.xls"); }public MemoryStream ExportQualifiedExaminees(string modelPath, string serviceName, IList<QualifiedExamineeEn> lst, string imgPath) { MemoryStream imageStream = new MemoryStream(); HSSFWorkbook workbook = new HSSFWorkbook(File.OpenRead(modelPath)); Image.FromFile(imgPath).Save(imageStream, System.Drawing.Imaging.ImageFormat.Png); byte[] bytes = new byte[imageStream.Length]; bytes = imageStream.GetBuffer(); imageStream = null; //int picInt = ; HSSFClientAnchor ac = new HSSFClientAnchor(0, 0, 0, 0, 0, 0, 1, 1); Row r = workbook.GetSheet("合格考生名单").GetRow(4); var style = r.GetCell(0).CellStyle; var type = r.GetCell(0).CellType; IEnumerable<IGrouping<string, QualifiedExamineeEn>> list = lst.GroupBy(m => m.ExamLocationName); for (int i = 0; i < list.Count(); i++) { List<QualifiedExamineeEn> ls = list.ElementAt(i).ToList<QualifiedExamineeEn>(); Sheet sheet = workbook.CloneSheet(0); workbook.SetSheetName(i + 1, ls[0].ExamLocationName); var patriarch = sheet.CreateDrawingPatriarch(); patriarch.CreatePicture(ac, workbook.AddPicture(bytes, PictureType.PNG)).Resize(); sheet.GetRow(2).GetCell(0).SetCellValue("考试服务名称:" + serviceName); sheet.GetRow(3).GetCell(0).SetCellValue("考场地址:" + ls[0].LocationAddress); int countFlag = 1; foreach (var m in ls) { var newRow = sheet.CreateRow(sheet.LastRowNum + 1); newRow.HeightInPoints = 18; if (m.IDNumber == null || m.IDNumber.Length == 0) { CreateCell(newRow, 0, style, type, countFlag.ToString()); CreateCell(newRow, 1, style, type, m.AdmissionFormId.ToString()); CreateCell(newRow, 2, style, type, ""); CreateCell(newRow, 3, style, type, ""); CreateCell(newRow, 4, style, type, ""); CreateCell(newRow, 5, style, type, ""); CreateCell(newRow, 6, style, type, ""); CreateCell(newRow, 7, style, type, ""); CreateCell(newRow, 8, style, type, ""); } else { CreateCell(newRow, 0, style, type, countFlag.ToString()); CreateCell(newRow, 1, style, type, m.AdmissionFormId.ToString()); CreateCell(newRow, 2, style, type, m.FirstName + " " + m.LastName); CreateCell(newRow, 3, style, type, m.Gender ? "男" : "女"); CreateCell(newRow, 4, style, type, m.Birthday); CreateCell(newRow, 5, style, type, m.Department); CreateCell(newRow, 6, style, type, m.IDNumber); CreateCell(newRow, 7, style, type, ""); CreateCell(newRow, 8, style, type, ""); } countFlag++; } } workbook.RemoveSheetAt(0); MemoryStream ms = new MemoryStream(); workbook.Write(ms); workbook = null; r = null; return ms; } IE6下如何处理图片宽度太大的问题? 怎么才能使日期写入数据库时是只有日期的形式 MVC中<%:%>用处 gridview 关闭网页窗口时,先执行button的click事件才关闭,代码怎么写?谢谢大家 使用水晶报表参数字段,出现"丢失参数字段当前值"错误,请高手多指教 一个关于正则表达式的问题 简单的入门级问题!请老鸟支持。------关于asp.net验证码 请看以下sql语句那里错了?大家帮忙 那里有反编译DLL工具可以下载呀! 关于List<T>列表问题,求解 关于farmeset的问题
或者你读取excel模板,往模板插入数据也可以。
using (MemoryStream m = bll.ExportQualifiedExaminees(Server.MapPath("~/Resources/考生签到表导出模版.xls"), list1[0].fServiceName, list, Server.MapPath("~/Common/Images/toeic_log.PNG")))
{
ExcelExportHandler.ExportFile(m, "application/ms-excel", "UTF-8", "GB2312", "考生签到表.xls");
}public MemoryStream ExportQualifiedExaminees(string modelPath, string serviceName, IList<QualifiedExamineeEn> lst, string imgPath)
{
MemoryStream imageStream = new MemoryStream();
HSSFWorkbook workbook = new HSSFWorkbook(File.OpenRead(modelPath));
Image.FromFile(imgPath).Save(imageStream, System.Drawing.Imaging.ImageFormat.Png);
byte[] bytes = new byte[imageStream.Length];
bytes = imageStream.GetBuffer(); imageStream = null;
//int picInt = ;
HSSFClientAnchor ac = new HSSFClientAnchor(0, 0, 0, 0, 0, 0, 1, 1);
Row r = workbook.GetSheet("合格考生名单").GetRow(4);
var style = r.GetCell(0).CellStyle;
var type = r.GetCell(0).CellType; IEnumerable<IGrouping<string, QualifiedExamineeEn>> list = lst.GroupBy(m => m.ExamLocationName); for (int i = 0; i < list.Count(); i++)
{
List<QualifiedExamineeEn> ls = list.ElementAt(i).ToList<QualifiedExamineeEn>(); Sheet sheet = workbook.CloneSheet(0);
workbook.SetSheetName(i + 1, ls[0].ExamLocationName);
var patriarch = sheet.CreateDrawingPatriarch(); patriarch.CreatePicture(ac, workbook.AddPicture(bytes, PictureType.PNG)).Resize();
sheet.GetRow(2).GetCell(0).SetCellValue("考试服务名称:" + serviceName);
sheet.GetRow(3).GetCell(0).SetCellValue("考场地址:" + ls[0].LocationAddress); int countFlag = 1;
foreach (var m in ls)
{
var newRow = sheet.CreateRow(sheet.LastRowNum + 1);
newRow.HeightInPoints = 18;
if (m.IDNumber == null || m.IDNumber.Length == 0)
{
CreateCell(newRow, 0, style, type, countFlag.ToString());
CreateCell(newRow, 1, style, type, m.AdmissionFormId.ToString());
CreateCell(newRow, 2, style, type, "");
CreateCell(newRow, 3, style, type, "");
CreateCell(newRow, 4, style, type, "");
CreateCell(newRow, 5, style, type, "");
CreateCell(newRow, 6, style, type, "");
CreateCell(newRow, 7, style, type, "");
CreateCell(newRow, 8, style, type, "");
}
else
{
CreateCell(newRow, 0, style, type, countFlag.ToString());
CreateCell(newRow, 1, style, type, m.AdmissionFormId.ToString());
CreateCell(newRow, 2, style, type, m.FirstName + " " + m.LastName);
CreateCell(newRow, 3, style, type, m.Gender ? "男" : "女");
CreateCell(newRow, 4, style, type, m.Birthday);
CreateCell(newRow, 5, style, type, m.Department);
CreateCell(newRow, 6, style, type, m.IDNumber);
CreateCell(newRow, 7, style, type, "");
CreateCell(newRow, 8, style, type, "");
}
countFlag++;
}
} workbook.RemoveSheetAt(0);
MemoryStream ms = new MemoryStream();
workbook.Write(ms);
workbook = null;
r = null; return ms;
}