XSSFWorkbook 多表头导出

发布时间 2023-09-15 11:31:08作者: 落榜的美术生
  IWorkbook workbook = new XSSFWorkbook();
  IFont bodyCellFont = workbook.CreateFont();
  bodyCellFont.FontName = "宋体";
  bodyCellFont.FontHeightInPoints = 12; //设置字体大小

  IFont columnHeadFont = workbook.CreateFont();
  columnHeadFont.FontHeightInPoints = 13; //设置字体大小
  columnHeadFont.FontName = "黑体"; //设置字体

  ICellStyle headCellStyle = workbook.CreateCellStyle();
  headCellStyle.SetFont(columnHeadFont);
  headCellStyle.BorderTop = BorderStyle.Thin;
  headCellStyle.BorderLeft = BorderStyle.Thin;
  headCellStyle.BorderRight = BorderStyle.Thin;
  headCellStyle.BorderBottom = BorderStyle.Thin;
  headCellStyle.Alignment = HorizontalAlignment.Center;
  headCellStyle.VerticalAlignment = VerticalAlignment.Center;

  ICellStyle cellStyle = workbook.CreateCellStyle();
  cellStyle.BorderTop = BorderStyle.Thin;
  cellStyle.BorderLeft = BorderStyle.Thin;
  cellStyle.BorderRight = BorderStyle.Thin;
  cellStyle.BorderBottom = BorderStyle.Thin;
  cellStyle.SetFont(bodyCellFont);

  ISheet sheet1 = workbook.CreateSheet(dtProcessDateStart.Value.ToString("yy-MM") + "工资合计");
  //设置列标题
  List<string> titleList = new List<string> { "工段", "员工编号", "员工姓名", "月工资" };
  List<string> titleList2 = new List<string> { "计件工资", "补差工资", "计时工资", "最终工资" };
  foreach (string monthDay in monthDayList)
  {
      titleList.Add(monthDay);
  }

  var rowTitle = sheet1.CreateRow(0);
  var rowTitle2 = sheet1.CreateRow(1);
  int cellIndex = 0;
  foreach (string title in titleList)
  {
      var j = 0;
      if (cellIndex < 4)
      {
          var cellT = rowTitle.CreateCell(cellIndex);
          cellT.SetCellValue(title);
          cellT.CellStyle = headCellStyle;
      //合并列 CellRangeAddress region
= new CellRangeAddress(0, 1, cellIndex, cellIndex); sheet1.AddMergedRegion(region); } else {
     //title 为天数,01,02,03,04...
var cellT = rowTitle.CreateCell(4 * int.Parse(title)); cellT.SetCellValue(title); cellT.CellStyle = headCellStyle;
      //天数下面插入4列
foreach (var title2 in titleList2) { var cellT2 = rowTitle2.CreateCell(4 * int.Parse(title) + j); cellT2.SetCellValue(title2); cellT2.CellStyle = headCellStyle; j++; }
      //合并行,天数行 开始行 StartRow 结束行 EndRow CellRangeAddress region
= new CellRangeAddress(0, 0, 4 * int.Parse(title), 4 * int.Parse(title) + 3); sheet1.AddMergedRegion(region); } if (cellIndex < 3) { sheet1.SetColumnWidth(cellIndex, 4000); } else { sheet1.SetColumnWidth(cellIndex, 2000); } cellIndex++; } //设置内容 int nextInsertIndex = 2; foreach (dynamic employee in employeeList) { List<string> rowData = new List<string>(); rowData.Add(employee.department_name.ToString()); rowData.Add(employee.account.ToString()); rowData.Add(employee.employee_name.ToString()); var statList = employeeWageStatList.Where(s => s.process_employee_id == employee.id); rowData.Add(statList.Sum(s => (decimal)s.day_final_wage).ToString()); foreach (string monthDay in monthDayList) { if (statList.FirstOrDefault(s => s.process_day == monthDay) != null) { rowData.Add(statList.FirstOrDefault(s => s.process_day == monthDay).day_process_wage.ToString()); rowData.Add(statList.FirstOrDefault(s => s.process_day == monthDay).day_additional_wage.ToString()); rowData.Add(statList.FirstOrDefault(s => s.process_day == monthDay).day_timing_wage.ToString()); rowData.Add(statList.FirstOrDefault(s => s.process_day == monthDay).day_final_wage.ToString()); } else { rowData.Add(""); rowData.Add(""); rowData.Add(""); rowData.Add(""); } } IRow row = sheet1.CreateRow(nextInsertIndex); for (int k = 0; k < rowData.Count; k++) { ICell cell = row.CreateCell(k); cell.CellStyle = cellStyle; if (!string.IsNullOrWhiteSpace(rowData[k]) && Regex.IsMatch(rowData[k], @"^[+-]?\d*[.]?\d*$")) //数字 { if (k > 2) { cell.SetCellValue(ConvertUtils.ToDouble(rowData[k], 0)); } else { cell.SetCellValue(rowData[k]); } } else { cell.SetCellValue(rowData[k]); } } nextInsertIndex++; }

效果图: