日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

ExcelReport第三篇:扩展元素格式化器

發布時間:2025/6/15 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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第三篇:扩展元素格式化器的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。