private static void CreateMergeHeader(ISheet sheet, ICellStyle style, Dictionary <int, List <MDataRow> > headerGroup, MDataTable header) { for (int i = 0; i < headerGroup.Count; i++) //事先创建好每一行数据,之后能才产生合并数据。 { IRow row = sheet.CreateRow(i); for (int d = 0; d < header.Rows.Count; d++) { ICell cel = row.CreateCell(d); cel.CellStyle = style; } } //设置标题和合并单元格。 foreach (KeyValuePair <int, List <MDataRow> > item in headerGroup) { IRow row = sheet.GetRow(item.Key); row.Height = 300; int cellIndex = 0; for (int i = 0; i < item.Value.Count; i++) { MDataRow data = item.Value[i]; int colspan = data.Get <int>(Config_Grid.Colspan, 1); int rowspan = data.Get <int>(Config_Grid.Rowspan, 1); string title = data.Get <string>(Config_Grid.Title); if (i == 0) { string field = data.Get <string>(Config_Grid.Field); cellIndex = header.GetIndex(Config_Grid.Field + "='" + field + "'"); if (cellIndex < 0) { cellIndex = 0; } } if (colspan > 1 || rowspan > 1) { sheet.AddMergedRegion(new CellRangeAddress(item.Key, item.Key + rowspan - 1, cellIndex, cellIndex + colspan - 1));//合并单元格 } ICell cell = row.GetCell(cellIndex, MissingCellPolicy.CREATE_NULL_AS_BLANK); sheet.SetColumnWidth(cell.ColumnIndex, 4000); cell.SetCellValue(title); cellIndex += colspan; } } }