ExcelReport第三篇:扩展元素格式化器
生活随笔
收集整理的這篇文章主要介紹了
ExcelReport第三篇:扩展元素格式化器
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
2019獨角獸企業重金招聘Python工程師標準>>>
導航
目?? 錄:基于NPOI的報表引擎——ExcelReport
上一篇:ExcelReport源碼解析
概述
上篇中已介紹了ExcelRepor的架構,本篇將通過例子講述如何擴展元素格式化器以滿足更多的需求。
示例
1)談談新需求:
如圖所示,一個單元格內包含多個參數。
2)實現代碼:
PartFormatter.cs:
/* 類:PartFormatter 描述:單元格局部(元素)格式化器 編 碼 人:韓兆新 日期:2015年01月25日 修改記錄: */ ? using System.Drawing; using NPOI.SS.UserModel; ? namespace ExcelReport { public class PartFormatter:ElementFormatter { private Point _cellPoint; private string _parameterName; private string _value; ? public PartFormatter(Point cellPoint, string parameterName ,string value) { this._cellPoint = cellPoint; this._parameterName = parameterName; this._value = value; } ? public override void Format(SheetFormatterContext context) { var rowIndex = context.GetCurrentRowIndex(_cellPoint.X); var row = context.Sheet.GetRow(rowIndex); if (null == row) { row = context.Sheet.CreateRow(rowIndex); } var cell = row.GetCell(_cellPoint.Y); if (null == cell) { cell = row.CreateCell(_cellPoint.Y); } if (cell.CellType.Equals(CellType.String)) { SetCellValue(cell, cell.StringCellValue.Replace(string.Format("$[{0}]", _parameterName), _value)); } } } }(PartFormatter繼承ElementFormatter,實現Format方法)。
3)測試代碼:
//實例化一個參數容器,并加載模板填充規則文件 ParameterCollection collection = new ParameterCollection(); collection.Load(@"Template\Template.xml"); ? //實例化一個元素格式化器列表 List<ElementFormatter> formatters = new List<ElementFormatter>(); formatters.Add(new PartFormatter(collection["Sheet1", "Dept"],"Dept","物理")); formatters.Add(new PartFormatter(collection["Sheet1", "Class"], "Class", "力學一")); ? //導出文件到本地 Export.ExportToLocal(@"Template\Template.xls", saveFileDlg.FileName, new SheetFormatterContainer("Sheet1", formatters));4)測試結果:
?
源碼下載:
下載地址:https://github.com/hanzhaoxin/ExcelReport
轉載于:https://my.oschina.net/hanzhaoxin/blog/373278
總結
以上是生活随笔為你收集整理的ExcelReport第三篇:扩展元素格式化器的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ANR
- 下一篇: AngularJS+Satellizer