全新 ReportX V3 可以用簡單的方法,快速建立 Word, Excel, Odt 與 Ods 報表。
你可以使用內建的快速產生器建立預設報表,也可以照自己的意思刻出客製化報表。
Package Manager
PM> Install-Package ReportX -Version 3.0.0
Nuget
v1.2.0
開始的版本僅支援 .NET Framework 4.5 以上ReportCreator
建立標準規格報表ReportX 提供了一個標準報表產生器 ReportCreator<T>
,它包含了:
將 <T>
帶入 Word
, Excel
, Odt
或 Ods
就可以產生相應格式的報表:
// 資料
ModelEmployeeTicket[] data = new ModelEmployeeTicket[] {
new ModelEmployeeTicket(){ postpid=10, name="zap"},
new ModelEmployeeTicket(){ postpid=11, name="jack"},
new ModelEmployeeTicket(){ postpid=12, name="peter"},
};
// 要顯示的欄位 (不一定要全部欄位都顯示)
string[] cols = new string[] { "ID", "姓名" };
// 報表標題
string title = "測試報表";
// 報表資料的時間範圍
DateTime date_from = DateTime.Now.AddDays(-1);
DateTime date_to = DateTime.Now;
// 建立報表人
string creator = "Administrator";
// 是否顯示資料總筆數
bool showTotal = true;
// 建立 Word 報表
ReportCreator<Word> report = new ReportCreator<Word>();
report.setInfo(data, cols, title, date_from, date_to, creator, showTotal);
// 報表結果字串,可以直接存檔成 .doc 即可瀏覽
string word = report.render();
加上 [Present("顯示名稱")]
屬性(Attribute) 設定欄位顯示名稱:
public class ModelEmployeeTicket
{
[Present("ID")]
public Int64 postpid { get; set; }
[Present("姓名")]
public string name { get; set; }
[Present("電話")]
public string tel { get; set; }
}
資料模型
ModelEmployeeTicket
可以自行定義
使用報表存檔類別 ReportFile
,將報表存成實際檔案
// 將報表物件傳入
ReportFile rf = new ReportFile(report.report);
string fileName = "我的報表";
// 儲存報表,將回傳報表的儲存路徑 (報表將會被存放在暫存區,你可以自行再搬移)
string path = rf.saveFile(fileName);
⚠️注意:
Word
與 Excel
的產生的報表結果支援網頁顯示 (屬於 HTML),Odt
與 Ods
不支援 (特殊格式的 XML)Odt
與 Ods
因為需要將 meta 檔案與報表內容檔進行 zip 壓縮後才可正常瀏覽,因此需要先存成實體檔案才可使用以下展示更多範例,您可自行參閱最符合需求的案例參考
部分範例將使用 Getting Started 章節中的
ModelEmployeeTicket
資料模型
OpenOffice 報表是由以下結構所構成:
META-INF
:設定檔存放資料夾
manifest.xml
:OpenOffice 文件設定檔content.xml
:報表內容因此,如果要瀏覽 OpenOffice 檔案,需要先將報表使用以上結構組成後 zip 壓縮,但 ReportX 提供了更簡易的方法:
ReportCreator<Ods> report = new ReportCreator<Ods>();
report.setInfo(data, cols, title, DateTime.Now.AddDays(-1), DateTime.Now, "測試人員", true);
// 將報表儲存
ReportFile rf = new ReportFile(report.report);
string fileName = "My_OpenOffice_Report";
string path = rf.saveFile(fileName); // 回傳存放路徑 (可再自行移動)
目前支援 Microsoft Excel 將多個報表整合成一個,使用 MultiExcelBundler
類別
// 建立第一張 Excel
ReportCreator<Excel> report1 = new ReportCreator<Excel>();
report1.setInfo(data, cols, "第一個Excel", DateTime.Now.AddDays(-1), DateTime.Now, "測試人員", true);
// 建立第二張 Excel
ReportCreator<Excel> report2 = new ReportCreator<Excel>();
report2.setInfo(data, cols, "第二個Excel", DateTime.Now.AddDays(-1), DateTime.Now, "測試人員", true);
// 綁定兩個 Excel
MultiExcelBundler bundler = new MultiExcelBundler();
bundler.addExcel(report1.report);
bundler.addExcel(report2.report);
// 儲存成實體檔案 (將 bundler 帶入)
ReportFile rf = new ReportFile(bundler);
string path = rf.saveFile(fileName);
DataTable
資料輸入在 Getting Started 章節中使用 ModelEmployeeTicket
資料模型做為資料儲存的容器。
此外,也可使用 DataTable
作為報表的資料輸入,使用上與資料模型並無差異,範例如下:
DataTable data = new DataTable("dTable");
// 設定欄位
DataColumn[] table_column ={
new DataColumn("ID",typeof(int)),
new DataColumn("姓名",typeof(string)),
new DataColumn("電話",typeof(string))
};
data.Columns.AddRange(table_column);
// 填充資料
for (int i = 0; i <= 4; i++)
{
var row = data.NewRow();
row["ID"] = i+1;
row["姓名"] = "SOL";
row["電話"] = "0923456789";
data.Rows.Add(row);
}
// 使用 DataTable 做為資料輸入
ReportCreator<Word> report = new ReportCreator<Word>();
report.setInfo(data, cols, title, DateTime.Now.AddDays(-1), DateTime.Now, "測試人員", true);
string word = report.render();
如果 ReportCreator
預設標準的報表格式不符合使用需求,如果要自訂報表,可以參考以下範例:
// 舉例:客製化 Word 報表的內容
Word report = new Word();
// 設定欄位資訊,將 data 的欄位填入
report.setCol(data);
// 設定要顯示在報表上的欄位
string[] cols = new string[] { "ID", "姓名" };
report.changecut(cols);
// 增加一個滿版橫列 (橫跨所有欄位)
string className = "r-header-title";
report.appendFullRow("增加一個 Title", null, className);
// 填充客製化樣式設定 (Word, Excel 使用 CSS;Odt, Ods 使用 XML)
string customOfficeCSS = @"
.r-header-title{
font-size: 22px;
font-weight: bold;
}"
report.setCustomStyle(customOfficeCSS);
// 使用 ModelTR 與 ModelTD 進行組裝,類似以下效果
// <tr>
// <td class='column'>資料1</td>
// <td class='column'>資料2</td>
// </tr>
ModelTR col = report.appendRow(new string[] { "資料1", "資料2" });
// 針對每個 td 設定 className 或是 style
foreach (ModelTD td in col.tds) td.className = "column";
// 將 data 資料填充到報表中
report.appendTable(data);
// 畫出自定的報表
string res = report.render();
❗ 注意,Odt
與 Ods
的樣式設定不是 CSS,他是由 XML 結構組成的設定,範例如下:
<office:automatic-styles>
<style:style style:name='ColumnWord' style:family='table-cell' style:parent-style-name='Default' style:data-style-name='N0'>
<style:text-properties fo:color='#FFFFFF' style:font-name='微軟正黑體' style:font-name-asian='微軟正黑體' style:font-name-complex='微軟正黑體'/>
</style:style>
<style:page-layout style:name='pm1'>
<style:page-layout-properties fo:margin-top='0.5in' fo:margin-bottom='0.5in' fo:margin-left='0.75in' fo:margin-right='0.75in' style:print-orientation='portrait' style:print-page-order='ttb' style:first-page-number='continue' style:scale-to='100%' style:table-centering='none' style:print='objects charts drawings'/>
<style:header-style>
<style:header-footer-properties fo:min-height='0.5in' fo:margin-left='0.75in' fo:margin-right='0.75in' fo:margin-bottom='0in'/>
</style:header-style>
<style:footer-style>
<style:header-footer-properties fo:min-height='0.5in' fo:margin-left='0.75in' fo:margin-right='0.75in' fo:margin-top='0in'/>
</style:footer-style>
</style:page-layout>
</office:automatic-styles>
<office:master-styles>
<style:master-page style:name='mp1' style:page-layout-name='pm1'>
<style:header/>
<style:header-left style:display='false'/>
<style:footer/>
<style:footer-left style:display='false'/>
</style:master-page>
</office:master-styles>
ReportX API 參考一覽
ReportCreator<T>
Class快速建立標準報表物件
T report
:報表物件 (T
可為 Word
,Excel
,Ods
,Odt
)ReportCreator()
void setInfo(R[] data, string[] cols, string title, DateTime from, DateTime? to = null, string creator = null, bool showTotal = false)
:設定報表資訊
data
:資料cols
:欲顯示的欄位title
:標題from
:資料開始時間to
:資料結束時間creator
:報表建立人showTotal
:是否顯示資料總數string render()
:畫出目前的報表,回傳報表字串結果
ReportFile
Class將報表字串存檔成實體檔案專用類別,支援 IReport 與 MultiExcelCreator 兩種規格的檔案產生
無
ReportFile(IReportX report)
report
:為 Word
,Excel
,Ods
,Odt
其中一種 classReportFile(MultiExcelBundler excel_creator)
excel_creator
:多個 excel 合成好的 classstring saveFile(string name, int? width = null)
:將報表儲存成實體檔案,並回傳儲存路徑
fileName
:報表名稱(不用副檔名)width
:寬度void deleteReportFile()
:如果報表已經不需要再使用,則可以呼叫此方法刪除檔案,否則需要自行刪除MultiExcelBundler
Class無
MultiExcelBundler()
void addExcel(Excel report)
:添加 Excel 報表
report
:Excel 報表string render(int? width = null)
:將多個 Excel 綁定成一個,並生成新的內容字串
width
:寬度Word
Class∟ 繼承:AbsOffice
Microsoft Office Word 底層操作類別
同 IReportX class 介紹
Word()
同 IReportX class 介紹
Excel
Class∟ 繼承:AbsOffice
Microsoft Office Excel 底層操作類別
同 IReportX class 介紹
Excel()
同 IReportX class 介紹
Odt
Class∟ 繼承:AbsOpenOffice
OpenOffice Odt 底層操作類別
string meta
:Ods file 專用 Meta 宣告,用於 META-INF 檔案建立時填入Odt()
同 AbsOpenOffice class 介紹
Ods
Class∟ 繼承:AbsOpenOffice
OpenOffice Ods 底層操作類別
string meta
:Ods file 專用 Meta 宣告,用於 META-INF 檔案建立時填入Ods()
同 AbsOpenOffice class 介紹
AbsOffice
與 AbsOpenOffice
Class∟ 繼承:IReport
定義 Office 與 OpenOffice 相關功能的抽象類別
abstract string[] oldcols
:原欄位位資訊abstract string[] newcols
:過濾後欄位資訊abstract string[] cols
:資料欄位資訊abstract List<ModelTR> trs
:報表每一列的詳細資訊抽象類別無法實例化
virtual string render(int? width = null)
:畫出報表,並回傳結果字串
width
:寬度abstract void changecut(string[] cut)
:過濾欲顯示欄位
cut
:欲顯示的欄位陣列abstract void setCustomStyle(string css)
:設定客製化 CSS 樣式abstract ModelTR appendFullRow(string data, string trStyle = null, string className = null)
:新增一個滿版列(橫跨所有欄)
data
:該列要顯示的內容trStyle
:該列的自訂樣式className
:該列的 classNameModelTR appendRow(params object[] data)
:新增一列,並在該列中填充數個欄位
data
:每一個欄位的設定,必須是一個陣列。其中,object
的規格如下:
object data
:要顯示的資料int colspan
:合併幾個欄,預設 1 無合併int rowspan
:合併幾個列,預設 1 無合併string fontSize
:字體大小string align
:對齊設定 (center, left, right)bool bold
:是否粗體string bgcolor
:背景顏色string style
:自訂樣式string className
:classNamevoid appendTable<T>(T[] data, string trStyle = null, string className = null)
:新增一個 Table 資料
data
:表格資料 (資料模型陣列)trStyle
:每列樣式className
:每列 classNamevoid appendTable(DataTable data, string trStyle = null, string className = null)
:新增一個 Table 資料
data
:表格資料 (DataTable)trStyle
:每列樣式className
:每列 classNamevoid setCol<T>(T[] data)
:設定資料欄位資訊 (呼叫 setReportColNum
)void setCol(DataTable data)
:設定資料欄位資訊 (呼叫 setReportColNum
)abstract void setReportColNum()
:設定欄位數量abstract void setData(string author = null, string company = null, string sheetName = null, string dateTime = null, string dateRange = null)
:設定報表背景資訊
author
:作者名稱company
:公司名稱sheetName
:報表名稱dateTime
:建立時間dateRange
:報表資料時間範圍IReportX
interface基底報表規格介面
string[] oldcols
:定義原欄位位資訊string[] newcols
:定義過濾後欄位資訊string[] cols
:定義資料欄位資訊string render(int? width = null)
:定義畫出報表,並回傳結果字串void changecut(string[] cut)
:定義過濾欲顯示欄位void setCustomStyle(string css)
:定義設定客製化 CSS 樣式ModelTR appendFullRow(string data, string trStyle = null, string className = null)
:定義新增一個滿版列(橫跨所有欄)ModelTR appendRow(params object[] data)
:定義新增一列,並在該列中填充數個欄位void appendTable<T>(T[] data, string trStyle = null, string className = null)
:定義新增一個 Table 資料void appendTable(DataTable data, string trStyle = null, string className = null)
:定義新增一個 Table 資料void setData(string author = null, string company = null, string sheetName = null, string dateTime = null, string dateRange = null)
:定義設定報表背景資訊int getColCount()
:定義取得欄位數量void setCol<T>(T[] data)
:定義設定欄位資訊void setCol(DataTable data)
:定義設定欄位資訊請聯絡以下 Email,我們會盡快給予協助
soldierzx0705@gmail.com
Copyright 2020 Zap Lin
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.