如何把DataGridView中的多维表头生成到Excel表头,格式不变

解决方案 »

  1.   

    如何导入数据至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;
            }