日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 編程語言 正文

C#使用DoddleReport快速生成報(bào)表_C#教程

作者:天方 ? 更新時(shí)間: 2022-08-11 編程語言

有的時(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 &copy; 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

欄目分類
最近更新