public void Export(Stream stream) { this._stream = stream; WriteEventArgs args = new WriteEventArgs() { ExSheet = null, Entity = Entity }; this.OnWrite(this, args); }
protected override void Writing(object sender, WriteEventArgs args) { foreach (var item in _components) { WriteEventArgs newArgs = new WriteEventArgs() { ExSheet = args.ExSheet, Entity = item.Entity }; item.OnWrite(item, newArgs); } }
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); }
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 EndWrite(WriteEventArgs args) { if (_book == null) { return; } RemoveDynamicSheet(_book); ShrinkSheet(_book); _book.Write(_stream); }
public virtual void OnWrite(object sender, WriteEventArgs args) { if (BeforeWrite != null) { BeforeWrite(sender, args); } Writing(sender, args); if (AfterWrite != null) { AfterWrite(sender, args); } }
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); } } }
protected override void EndWrite(WriteEventArgs args) { ISheet exSheet = args.ExSheet; IWorkbook book = exSheet.Workbook; exSheet.ForceFormulaRecalculation = true; //删除模板 if (IsDynamic && !Sheet.KeepTemplate) { int index = book.GetSheetIndex(exSheet); if (index > -1) { book.RemoveSheetAt(book.GetSheetIndex(exSheet)); } } //Sheet更名条件:非动态Sheet且指定了NameRule属性 if (!IsDynamic && !string.IsNullOrEmpty(Sheet.NameRule)) { book.SetSheetName(book.GetSheetIndex(exSheet), Sheet.GetExportName()); } base.EndWrite(args); //WriteNodes(_exSheet, NodesAddAfter); }
protected abstract void Writing(object sender, WriteEventArgs args);
/// <summary> /// 渲染后的结束工作(最后一步处理) /// </summary> protected virtual void EndWrite(WriteEventArgs args) { }
/// <summary> /// 初始化(第一步处理) /// </summary> /// <param name="args"></param> public virtual void PreWrite(WriteEventArgs args) { }
public override void OnWrite(object sender, WriteEventArgs args) { PreWrite(args); base.OnWrite(this, args); EndWrite(args); }
protected override void Writing(object sender, WriteEventArgs args) { Cell cell = args.Entity as Cell; ISheet exSheet = args.ExSheet; if (cell == null) { return; } object tmpObject = this.GetValue(); if (tmpObject == null) { return; } IWorkbook book = exSheet.Workbook; //IDrawing draw = exSheet.DrawingPatriarch ?? exSheet.CreateDrawingPatriarch(); IDrawing draw = exSheet.DrawingPatriarch ?? exSheet.CreateDrawingPatriarch();//只能有一个实例,否则只有最后一个对象生效 int rowIndex = this.RowIndex; int colIndex = this.ColIndex; IRow exRow = exSheet.GetRow(rowIndex) ?? exSheet.CreateRow(rowIndex); //if (exRow != null) //{ ICell exCell = exRow.GetCell(this.ColIndex) ?? exRow.CreateCell(this.ColIndex); //if (exCell != null) //{ //object tmpObject = sheet.IsDynamic ? sheet.GetValue(cell,sheetName) : dt.Rows[cell.DataIndex][cell.Field]; if (cell.ValueAppend) { tmpObject = exCell.StringCellValue + tmpObject; } if (tmpObject.GetType() == typeof(byte[]))//处理图片 { CellRangeAddress region = NPOIExcelUtil.GetRange(exCell); Image image = new Bitmap(new MemoryStream(tmpObject as byte[])); Size size = image.Size; if (size.IsEmpty) { return; } IClientAnchor anchor = region != null? draw.CreateAnchor(0, 0, 0, 0, region.FirstColumn, region.FirstRow, region.LastColumn + 1, region.LastRow + 1) : draw.CreateAnchor(20, 20, 0, 0, colIndex, rowIndex, colIndex + this.ColCount, rowIndex + this.RowCount); IPicture pic = draw.CreatePicture(anchor, book.AddPicture((byte[])tmpObject, PictureType.JPEG)); switch (cell.FillType) { case FillType.Origin: pic.Resize(); break; case FillType.Scale: float width = 0, height = 0; for (int i = anchor.Col1; i < anchor.Col2; i++) { width += exSheet.GetColumnWidth(i) / 256f * 12; } for (int i = anchor.Row1; i < anchor.Row2; i++) { IRow row = exSheet.GetRow(i); height += row != null ? row.HeightInPoints : exSheet.DefaultRowHeightInPoints; } float factor = Math.Min(width / (size.Width * 0.75f), height / (size.Height * 0.75f)); pic.Resize(factor); break; default: break; } } else { exCell.SetCellValue((tmpObject ?? "").ToString()); } //} //} }
public override void PreWrite(WriteEventArgs args) { //WriteNodes(_exSheet, NodesAddAfter); args.ExSheet = _exSheet; base.PreWrite(args); }
protected override void Writing(object sender, WriteEventArgs args) { WriteNodes(args.ExSheet, _nodes); }