Exemple #1
0
 protected override void SetColumnHead(NPOI.SS.UserModel.ISheet sheet, ref int rowIndex)
 {
     if (this.ColumnHeadList.Count > 0)
     {
         // 冻结
         sheet.CreateFreezePane(1, 4);
         // 数据从第3行开始显示
         rowIndex = rowIndex + 2;
         // 所有列头居中
         this.HeadStyle.Alignment = HorizontalAlignment.Center;
         for (int i = 0; i < 2; i++)
         {
             IRow row = sheet.CreateRow(rowIndex);
             foreach (ColumnsMapping cm in this.ColumnHeadList)
             {
                 ICell cell = null;
                 if (i == 0)
                 {
                     if (cm.ColumnsIndex < 3 || cm.ColumnsIndex == 13 || cm.ColumnsIndex == 14)
                     {
                         // 合并行
                         sheet.AddMergedRegion(new CellRangeAddress(rowIndex, rowIndex + 1, cm.ColumnsIndex, cm.ColumnsIndex));
                         cell = row.CreateCell(cm.ColumnsIndex);
                         // 设置列宽
                         SetColumnsWidth(sheet, cm.ColumnsIndex, cm.Width);
                         // 设置列头样式
                         cell.CellStyle = this.HeadStyle;
                         cell.SetCellValue(cm.ColumnsText);
                     }
                     else if (cm.ColumnsIndex == 3 || cm.ColumnsIndex == 8)
                     {
                         sheet.AddMergedRegion(new CellRangeAddress(rowIndex, rowIndex, cm.ColumnsIndex, cm.ColumnsIndex + 4));
                         cell = row.CreateCell(cm.ColumnsIndex);
                         SetColumnsWidth(sheet, cm.ColumnsIndex, cm.Width);
                         cell.CellStyle = this.HeadStyle;
                         if (cm.ColumnsIndex == 3)
                         {
                             cell.SetCellValue("成本");
                         }
                         else if (cm.ColumnsIndex == 8)
                         {
                             cell.SetCellValue("收入");
                         }
                         for (int j = 3; j <= 12; j++)
                         {
                             if (j == 3 || j == 8)
                             {
                                 continue;
                             }
                             cell           = row.CreateCell(j);
                             cell.CellStyle = this.HeadStyle;
                         }
                     }
                 }
                 else
                 {
                     if (cm.ColumnsIndex >= 3 && cm.ColumnsIndex <= 12)
                     {
                         cell           = row.CreateCell(cm.ColumnsIndex);
                         cell.CellStyle = this.HeadStyle;
                         SetColumnsWidth(sheet, cm.ColumnsIndex, cm.Width);
                         cell.SetCellValue(cm.ColumnsText);
                     }
                     else if (cm.ColumnsIndex < 3 || cm.ColumnsIndex == 13 || cm.ColumnsIndex == 14)
                     {
                         cell           = row.CreateCell(cm.ColumnsIndex);
                         cell.CellStyle = this.HeadStyle;
                     }
                 }
             }
             rowIndex++;
         }
     }
 }