zoukankan      html  css  js  c++  java
  • 用DoddleReport快速生成报表

    有的时候,我们需要对一堆数据进行统计分析后生成HTML或Excel格式报表。本来这并不是一件很难的事,但确是件比较麻烦的事情。最令人头痛的是遇到领导下发的临时紧急任务的时候,往往领导都不知道到底要什么报表,只是给你一堆数据先让你出一个分析报告,当你上缴分析报告后,领导会针对分析结果让你再出一个分析报告… 。这时要是有一个快速生成报表的工具就非常方便了。

    现在,我们可以通过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 &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. 对输出的字段进行格式控制


        // 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格式的报表,非常强大而方便。

     

  • 相关阅读:
    《C++ Primer(中文版)(第5版)》斯坦利·李普曼 (Stanley B. Lippman) (作者), 约瑟·拉乔伊 (Josee Lajoie) (作者), 芭芭拉·默 (Barbara E. Moo) (作者) azw3
    《O’Reilly精品图书系列共21册》azw3
    《跟老齐学Python:从入门到精通》齐伟(编著)epub+mobi+azw3
    《C Primer Plus(第6版)(中文版)》普拉达(作者)epub+mobi+azw3
    《算法图解》[美] Aditya Bhargava(作者)epub+mobi
    《程序之美系列(套装共6册)》[美]斯宾耐立思 等 (作者) epub+mobi+azw3
    《算法技术手册》George T. Heineman(作者)epub+mobi+azw3
    收藏夹
    笔记
    Unity3D物理引擎Rigidbody,Collider,PhysicMaterial的整理 [转]
  • 原文地址:https://www.cnblogs.com/TianFang/p/3130239.html
Copyright © 2011-2022 走看看