C#使用DoddleReport快速生成报表
作者:天方 时间:2022-05-31 06:39:59
有的时候,我们需要对一堆数据进行统计分析后生成HTML或Excel格式报表。本来这并不是一件很难的事,但确是件比较麻烦的事情。最令人头痛的是遇到领导下发的临时紧急任务的时候,往往领导都不知道到底要什么报表,只是给你一堆数据先让你出一个分析报告,当你上缴分析报告后,领导会针对分析结果让你再出一个分析报告… 。这时要是有一个快速生成报表的工具就非常方便了。
使用nuget安装控件
-install package DoddleReport
-install package DoddleReport.iTextSharp
现在,我们可以通过DoddleReport来快速生成报表。一个简单的示例如下:
假定我们的数据对象为如下格式:
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; }
}
数据源通如下函数生成:
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)
});
}
要对该数据源生成报表,则只需要如下几步:
1. 通过ToReportSource扩展函数将数据源转换为Report对象
// Create the report and turn our query into a ReportSource
var report = new Report(GetAllData().ToList().ToReportSource());
2. 添加报表的标题和页眉页脚的描述
// 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. 对输出的字段进行格式控制
// Customize the data fields
report.DataFields["Id"].Hidden = true;
report.DataFields["Price"].DataFormatString = "{0:c}";
report.DataFields["LastPurchase"].DataFormatString = "{0:d}";
4. 输出为报表
using (var outputStream = File.Create(@"r:\report.html"))
{
var writer = new DoddleReport.Writers.HtmlReportWriter();
writer.WriteReport(report, outputStream);
}
这样,我们就可以得到如下的报表:
使用起来非常简单,但基本上该有的都有,还是非常不错的。其中2,3两步是可以省略的,最简单的方式下,只要如下几行即可:
// 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中输出成在线报表。例如,我们只要把上面例子中的"Writers.HtmlReportWriter"改成"OpenXml.ExcelReportWriter"就可以生成Excel格式的报表,非常强大而方便。
来源:https://www.cnblogs.com/TianFang/archive/2013/06/09/3130239.html
标签:C#,DoddleReport,生成,报表
0
投稿
猜你喜欢
Java实现发送邮件并携带附件
2023-07-23 19:02:25
浅谈Spring中Bean的作用域、生命周期
2023-11-14 02:44:21
Java 反射(Reflect)详解
2022-09-27 08:34:44
opencv 做人脸识别 opencv 人脸匹配分析
2023-07-09 06:34:44
详解spring mvc中url-pattern的写法
2023-11-11 07:30:58
C#使用base64对字符串进行编码和解码的测试
2022-09-21 23:08:59
Java中过滤器 (Filter) 和 拦截器 (Interceptor)的使用
2023-07-07 00:20:28
spring mvc 组合mybatis框架实例详解
2023-11-28 04:56:04
SpringBoot Mybatis动态数据源切换方案实现过程
2022-10-12 03:25:03
一文搞懂Mybatis-plus的分页查询操作
2023-11-25 10:23:17
使用AOP的@Around后无返回值的解决
2023-11-24 13:04:09
C#构建树形结构数据(全部构建,查找构建)
2022-07-22 12:22:52
shiro多验证登录代码实例及问题解决
2023-11-30 07:20:00
关于Java中的try-with-resources语句
2022-10-21 16:49:54
java使用jacob实现word转pdf
2023-05-12 11:45:33
使用Jitpack发布开源Java库的详细流程
2021-12-18 06:55:58
Mybatis Lombok使用方法与复杂查询介绍
2023-03-30 00:30:13
java 重载(overload)与重写(override)详解及实例
2021-06-18 15:59:45
java实现实时通信聊天程序
2023-10-18 09:08:06
java实现表单必填参数验证的方法
2023-06-16 21:41:24