protected override void Writing(object sender, WriteEventArgs args) { ISheet exSheet = args.ExSheet; Table table = args.Entity as Table; if (table == null) { return; } Source source = this.Source; if (source == null) { return; } DataTable dt = source.Table; if (dt == null) { return; } ////根据XML指定区域与数据源,计算实际可填充区域(table根据字段数与字段位置或指定区域自动获得区域) int rowCount = this.RowCount; //table.Location.RowCount == 0 ? dt.Rows.Count : table.Location.RowCount; int colCount = this.ColCount; //列数必须由XML决定(指定区域或根据字段[位置或数量]计算) int rowIndexBase = this.RowIndex; //table.Location.RowIndex + increasedRowCount;//XML是根据模板设置,要加上填充区域的基数 int colIndexBase = this.ColIndex; IRow styleRow = exSheet.GetRow(rowIndexBase) ?? exSheet.CreateRow(rowIndexBase); //if (styleRow == null) return; //1、暂时移除区域中的合并单元格 Dictionary <int, int> dict = ClearMergeRegion(styleRow, colIndexBase, colIndexBase + colCount - 1); if (table.CopyFill && rowCount > 1) { NPOIExcelUtil.CopyRow(exSheet, rowIndexBase, rowIndexBase + 1, rowCount - 1); } IList <OutputNode> nodes = this.GetNodes(); //2、根据合并单元格调整结点区域 for (int i = 0; i < nodes.Count; i++) { //在行区域范围内,有合并单元格且导出规则未指定合并时,需要以合并单元格为准 if (rowIndexBase <= nodes[i].RowIndex && nodes[i].RowIndex < rowIndexBase + rowCount && dict.ContainsKey(nodes[i].ColumnIndex) && nodes[i].ColumnSpan == 1) { nodes[i].ColumnSpan = dict[nodes[i].ColumnIndex]; } } //3、将数据写入Sheet NodeWriter.WriteNodes(exSheet, nodes); }
protected override void Writing(object sender, WriteEventArgs args) { ISheet exSheet = args.ExSheet; RegionTable headerTable = args.Entity as RegionTable; if (headerTable == null) { return; } int colIndex = this.ColIndex, rowIndex = this.RowIndex, colCount = this.ColCount, rowCount = this.RowCount; int colHeaderLevel = headerTable.ColumnHeaderLevel; int rowHeaderLevel = headerTable.RowHeaderLevel; //1、Excel格式准备 ICell exCell = GetStandardCell(exSheet, RowIndex, colIndex, true); NPOIExcelUtil.CopyCell(exSheet, exCell, exCell.ColumnIndex + 1, colCount - 1); NPOIExcelUtil.CopyRow(exSheet, exCell.RowIndex, exCell.RowIndex + 1, rowHeaderLevel - 1); exCell = GetStandardCell(exSheet, rowIndex + rowHeaderLevel, colIndex); NPOIExcelUtil.CopyCell(exSheet, exCell, exCell.ColumnIndex + 1, colCount - 1); NPOIExcelUtil.CopyRow(exSheet, exCell.RowIndex, exCell.RowIndex + 1, rowCount - rowHeaderLevel - 1); //2、数据填充 IList <OutputNode> nodes = this.GetNodes(); foreach (var node in nodes) { IRow exRow = exSheet.GetRow(node.RowIndex) ?? exSheet.CreateRow(node.RowIndex); //if (exRow == null) continue; exCell = exRow.GetCell(node.ColumnIndex) ?? exRow.CreateCell(node.ColumnIndex); //if (exCell == null) continue; if (node.IsRegion) { exSheet.AddMergedRegion(new CellRangeAddress(node.RowIndex, node.RowIndex + node.RowSpan - 1, node.ColumnIndex, node.ColumnIndex + node.ColumnSpan - 1)); } NPOIExcelUtil.SetCellValueByDataType(exCell, node.Content); } //3、自适应宽度 for (int i = 0; i < this.ColCount; i++) { int endRow = this.RowIndex + (i < colHeaderLevel || rowHeaderLevel == 0 ? this.RowCount : rowHeaderLevel) - 1; NPOIExcelUtil.AutoFitColumnWidth(exSheet, colIndex + i, rowIndex, endRow); } //4、锁定标题区域(只有列标题、只有行标题、多行多列标题三种情况锁定不一样) if (headerTable.Freeze) { NPOIExcelUtil.Freeze(exSheet, rowHeaderLevel > 0 ? rowIndex + rowHeaderLevel : 0, colHeaderLevel > 0 ? colIndex + colHeaderLevel : 0); } NPOIExcelUtil.SetAreaBorder(exSheet, rowIndex, colIndex, rowCount, colCount); }
public override void PreWrite(WriteEventArgs args) { base.PreWrite(args); ISheet exSheet = args.ExSheet; DynamicArea dynamicArea = args.Entity as DynamicArea; if (dynamicArea == null) { return; } int baseRow = this.RowIndex; for (int i = 1; i < this.Count; i++) { for (int j = 0; j < dynamicArea.Location.RowCount; j++) { NPOIExcelUtil.CopyRow(exSheet, baseRow + j, baseRow + j + dynamicArea.Location.RowCount * i); } } }