網(wǎng)站首頁 編程語言 正文
有的時(shí)候,我們需要對(duì)一堆數(shù)據(jù)進(jìn)行統(tǒng)計(jì)分析后生成HTML或Excel格式報(bào)表。本來這并不是一件很難的事,但確是件比較麻煩的事情。最令人頭痛的是遇到領(lǐng)導(dǎo)下發(fā)的臨時(shí)緊急任務(wù)的時(shí)候,往往領(lǐng)導(dǎo)都不知道到底要什么報(bào)表,只是給你一堆數(shù)據(jù)先讓你出一個(gè)分析報(bào)告,當(dāng)你上繳分析報(bào)告后,領(lǐng)導(dǎo)會(huì)針對(duì)分析結(jié)果讓你再出一個(gè)分析報(bào)告… 。這時(shí)要是有一個(gè)快速生成報(bào)表的工具就非常方便了。
使用nuget安裝控件
-install package DoddleReport
-install package DoddleReport.iTextSharp
現(xiàn)在,我們可以通過DoddleReport來快速生成報(bào)表。一個(gè)簡(jiǎn)單的示例如下:
假定我們的數(shù)據(jù)對(duì)象為如下格式:
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public double Price { get; set; }
public int OrderCount { get; set; }
public DateTime LastPurchase { get; set; }
public int UnitsInStock { get; set; }
}
數(shù)據(jù)源通如下函數(shù)生成:
static IEnumerable<Product> GetAllData()
{
var rand = new Random();
return Enumerable.Range(1, 20).Select(
i => new Product
{
Id = i,
Name = "Product " + i,
Description = "This is an example description",
Price = rand.NextDouble() * 100,
OrderCount = rand.Next(1000),
LastPurchase = DateTime.Now.AddDays(rand.Next(1000)),
UnitsInStock = rand.Next(0, 2000)
});
}
要對(duì)該數(shù)據(jù)源生成報(bào)表,則只需要如下幾步:
1. 通過ToReportSource擴(kuò)展函數(shù)將數(shù)據(jù)源轉(zhuǎn)換為Report對(duì)象
// Create the report and turn our query into a ReportSource
var report = new Report(GetAllData().ToList().ToReportSource());
2. 添加報(bào)表的標(biāo)題和頁眉頁腳的描述
// Customize the Text Fields
report.TextFields.Title = "Products Report";
report.TextFields.SubTitle = "This is a sample report showing how Doddle Report works";
report.TextFields.Footer = "Copyright 2011 © The Doddle Project";
report.TextFields.Header = string.Format(@" Report Header Demo");
// Render hints allow you to pass additional hints to the reports as they are being rendered
report.RenderHints.BooleanCheckboxes = true;
3. 對(duì)輸出的字段進(jìn)行格式控制
// Customize the data fields
report.DataFields["Id"].Hidden = true;
report.DataFields["Price"].DataFormatString = "{0:c}";
report.DataFields["LastPurchase"].DataFormatString = "{0:d}";
4. 輸出為報(bào)表
using (var outputStream = File.Create(@"r:\report.html"))
{
var writer = new DoddleReport.Writers.HtmlReportWriter();
writer.WriteReport(report, outputStream);
}
這樣,我們就可以得到如下的報(bào)表:
使用起來非常簡(jiǎn)單,但基本上該有的都有,還是非常不錯(cuò)的。其中2,3兩步是可以省略的,最簡(jiǎn)單的方式下,只要如下幾行即可:
// Create the report and turn our query into a ReportSource
var report = new Report(GetAllData().ToList().ToReportSource());
using (var outputStream = File.Create(@"r:\report.html"))
{
var writer = new DoddleReport.Writers.HtmlReportWriter();
writer.WriteReport(report, outputStream);
}
值得一提的是,DoddleReport支持的輸出給事非常豐富:PDF、EXCEL、HTML、CSV等常用的格式都支持,也能直接在Asp.net中輸出成在線報(bào)表。例如,我們只要把上面例子中的"Writers.HtmlReportWriter"改成"OpenXml.ExcelReportWriter"就可以生成Excel格式的報(bào)表,非常強(qiáng)大而方便。
原文鏈接:https://www.cnblogs.com/TianFang/archive/2013/06/09/3130239.html
相關(guān)推薦
- 2022-08-02 Oracle數(shù)據(jù)庫丟失表排查思路實(shí)戰(zhàn)記錄_oracle
- 2023-01-20 python如何求兩數(shù)之和及多數(shù)之和_python
- 2022-07-27 python?[::-1]?[::-1,::-1]的具體使用_python
- 2022-08-13 從源碼理解SpringBootServletInitializer的作用
- 2022-02-27 解決 idea突然使用debug功能時(shí)項(xiàng)目啟動(dòng)一半卡住沒反應(yīng)也不報(bào)錯(cuò)
- 2022-08-18 go-spew調(diào)試?yán)髟斀鈅Golang
- 2023-04-27 React中關(guān)于render()的用法及說明_React
- 2022-12-08 C語言如何實(shí)現(xiàn)成績(jī)等級(jí)判別_C 語言
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支