/// <summary> /// 生成数据列表主要内容 /// </summary> /// <param name="cells"></param> /// <param name="sheetDataGrid">列表内容数据</param> /// <param name="mergeCellByCols">要合并的列</param> /// <param name="rowIndex">记录当前sheet的末尾行索引,改方法返回的rowIndex的值最后一行所在行索引</param> static void GenerateExcelGridContent(Cells cells, SheetDataGrid sheetDataGrid, Dictionary <string, string> mergeCellByCols, ref int rowIndex) { if (sheetDataGrid == null || sheetDataGrid.ContentItems == null) { return; } if (sheetDataGrid.DataGridColumn == null || sheetDataGrid.DataGridColumn.ShowColumns == null) { return; } List <string> columns = sheetDataGrid.DataGridColumn.ShowColumns.Select(o => o.MappingValue).ToList(); if (columns == null || !columns.Any()) { return; } DataTable dtContent = sheetDataGrid.ContentItems; rowIndex = sheetDataGrid.StartRowIndex; DataRow preDataRow = null; foreach (DataRow row in dtContent.Rows) { for (var col = 0; col < columns.Count; col++) { if (string.IsNullOrEmpty(columns[col])) { continue; } var cellValue = row[columns[col]] == null ? "" : row[columns[col]].ToString(); ExcelCellContent.SetCellContent(cellValue, sheetDataGrid.ContentStyle ?? ExcelCellContent.ContentStyle, cells[rowIndex, col], (sheetDataGrid.ContentRowHeight <= 0 ? 20 : sheetDataGrid.ContentRowHeight)); if (dtContent.Columns.Contains("IsMerge") && row["IsMerge"] != null && Convert.ToBoolean(row["IsMerge"]) == false) { continue; } if (mergeCellByCols != null && mergeCellByCols.Keys.Contains(columns[col])) { List <string> mergeCellByColsList = new List <string>(); if (!string.IsNullOrEmpty(mergeCellByCols[columns[col]])) { mergeCellByColsList.AddRange(mergeCellByCols[columns[col]].Split(',')); } if (!mergeCellByColsList.Contains(columns[col])) { mergeCellByColsList.Add(columns[col]); } MergeCell(cells, dtContent, preDataRow, row, mergeCellByColsList, rowIndex, col); } } rowIndex++; preDataRow = row; } rowIndex = rowIndex - 1;//减去循环完后新增的多余一行索引 }
/// <summary> /// 生成excel的标题行 /// </summary> /// <param name="cells"></param> /// <param name="titles">标题集合,根据定义的CellContent值可以指定标题行所在位置,样式等属性</param> /// <param name="rowIndex">记录当前sheet的末尾行索引,改方法返回的rowIndex的值最后一行所在行索引</param> static void GenerateExcelTitle(Cells cells, List <CellContent> titles, ref int rowIndex) { if (titles == null || !titles.Any()) { return; } foreach (var obj in titles) { if (obj == null) { continue; } cells.CreateRange(obj.RowIndex, obj.ColumnIndex, obj.TotalRows, obj.TotalColumns).Merge(); ExcelCellContent.SetCellContent(obj.CellValue, obj.CellStyle, cells[obj.RowIndex, obj.ColumnIndex], (obj.RowHeight <= 0 ? 20 : obj.RowHeight)); rowIndex = obj.RowIndex; } }
/// <summary> /// 生成sheet底部显示内容 /// </summary> /// <param name="cells"></param> /// <param name="footContent">定义的底部显示内容、样式等信息</param> /// <param name="rowIndex">记录当前sheet的末尾行索引,改方法返回的rowIndex的值最后一行所在行索引</param> static void GenerateExcelFootContent(Cells cells, List <CellContent> footContent, ref int rowIndex) { if (footContent == null || !footContent.Any()) { return; } foreach (var obj in footContent) { if (obj == null) { continue; } cells.CreateRange(obj.RowIndex + rowIndex, obj.ColumnIndex, obj.TotalRows, obj.TotalColumns).Merge(); ExcelCellContent.SetCellContent(obj.CellValue, cells[obj.RowIndex + rowIndex, obj.ColumnIndex], (obj.RowHeight <= 0 ? 20 : obj.RowHeight)); for (var i = 0; i < obj.TotalColumns; i++) { cells[obj.RowIndex + rowIndex, obj.ColumnIndex + i].SetStyle(obj.CellStyle); } } }
/// <summary> /// 生成数据列表在Sheet 显示的列头 /// </summary> /// <param name="cells"></param> /// <param name="dgColumn">定义的每一列属性</param> /// <param name="rowIndex">记录当前sheet的末尾行索引,改方法返回的rowIndex的值最后一行所在行索引</param> static void GenerateExcelColumnTitle(Cells cells, DataGridColumn dgColumn, ref int rowIndex) { if (dgColumn == null) { return; } if (dgColumn.ShowColumns == null || !dgColumn.ShowColumns.Any()) { return; } foreach (var obj in dgColumn.ShowColumns) { if (obj == null) { continue; } cells.CreateRange(obj.RowIndex, obj.ColumnIndex, (obj.TotalRows <= 0 ? 1 : obj.TotalRows), (obj.TotalColumns <= 0 ? 1 : obj.TotalColumns)).Merge(); ExcelCellContent.SetCellContent(obj.CellValue, obj.CellStyle, cells[obj.RowIndex, obj.ColumnIndex], (obj.RowHeight <= 0 ? 20 : obj.RowHeight)); rowIndex = obj.RowIndex; } }