zoukankan      html  css  js  c++  java
  • 在 .Net 项目中生成Report小记

    背景

    项目为WinForm + WCF 的应用,按照给定格式生成Report,显示在WinForm窗体上并可以导出为PDF和Excel文件。

    分析

    之前用过DevExpress For WinForm的报表控件,其包含了导出为PDF和Excel的功能,该项目没有相关软件授权,需要寻找其他解决方案。

    在得到Report结构和样式后,第一时间想到了HTML,网页可以实现丰富的样式(颜色、字号、单元格合并与拆分等),开发效率也高。基于 HTML Template,生成WinForm Report 和PDF Report不再是难题,这两个报表的实现是比较顺利的。

    接下来的Report都是在WCF中生成,以服务形式提供。

    实现

    创建HTML Template(示意图)

    实现WinForm Report

    使用HtmlAgilityPack填充HTML Template,生成的HTML Report可以通过WebBrowser显示在Winform中。

    实现PDF Report

    利用NReco.PdfGenerator可以将HTML Report转存为PDF格式文件。

    实现Excel Report

    这个部分的的实现比较纠结,兜兜转转的,我们一起Happy一下。

    1. 尝试将 HTML转存为 Excel。 失 败

    参考:https://www.watch-life.net/programming-notes/html-table-2-excel.html

    限制:每次打开都会显示 "文件可能损坏或不安全,除非信任来源" 弹窗。

    2. 尝试将 PDF转存为 Excel。 失 败

    1)参考:Online2PDF.comhttps://online2pdf.com/

         限制:1. 需要上传数据,不安全 2. 只能在线访问,我需要离线使用。

    2)参考:GemBox.Spreadsheet Free Versionhttps://www.gemboxsoftware.com/spreadsheet/free-version

         限制:该SDK免费版有数量限制(sheets per workbook <= 5,rows per sheet <= 150),我需要导出的行数大于150。

    3. 尝试免费版 XLSX SDK。 失 败

    参考:Templaterhttp://templater.info/

    限制:UNLICENSED (FREE) VERSION必须保留"Unlicensed version"提示,不得移除。我需要移除这个提示。

    插曲:关于"Unlicensed version"的提示,技术上移除掉没有难度,在跟SDK开发商咨询后,明确了该提示信息不得以任何方式移除,尊重版权,继续另寻他法。

    2. GRANT OF LICENSE http://templater.info/eula 
    2.1 UNLICENSED (FREE) VERSION of SOFTWARE. 
    LICENSEE is allowed to use SOFTWARE in unlimited number of computers, for any purpose, including use in commercial applications. However, LICENSEE is required to: a) honor and conform to usage limitations of free version and b) acknowledge usage of free version of the SOFTWARE in their application. Format, formulation and placement of acknowledgement are arbitrary, as long as it mentions LICENSOR. 
    LICENSEE may not remove INFO MESSAGE specific for unlicensed version. 
    LICENSOR does not have responsibility to provide any technical or any other support to the users of the free version of the SOFTWARE.

    4. 尝试开源OpenXML SDK。 退 缩
    参考:SpreadsheetLighthttp://spreadsheetlight.com/
    限制:手工计算并填充每一个单元格,代码逻辑复杂,我更恐惧这种方式的效率和工作量。

    5. 使用 XML Spreadsheet 2003 (*.xml) 模板。 成 功

    优势

    1) 可视化方式创建Excel Template,与 HTML Template异曲同工。

    2) 代码复用性高,与其他Report共用数据结构。

    3) 数据填充效率高,生成速度快。

    不足

    不支持某些高级特性(可能是宏,引用等,暂未用到)。

    实现

    1) 新建Excel 文档,设置占位符。

    2) 另存为 XML Spreadsheet 2003 (*.xml) 格式。

    3) 加载 xml 模板文本,使用HtmlAgilityPack替换字符串的方式填充数据,转存为xlsx格式文件。

    4) 使用 FreeSpire.XLS 控件将 XML 模板转换为 Excel 文件。
    https://www.nuget.org/packages/FreeSpire.XLS

  • 相关阅读:
    替代 Hystrix,Spring Cloud Alibaba Sentinel 快速入门
    官方文档中文版!Spring Cloud Stream 快速入门
    生产环境当然要高可用,Alibaba Nacos 集群部署
    对标Eureka的AP一致性,Nacos如何实现Raft算法
    抢占云原生市场,阿里开源服务发现组件 Nacos快速入门
    使用阿里云 ECS 快速部署 WordPress 博客系统
    Spring Cloud Alibaba 实战 之 Nacos 服务注册和发现
    ASPOSE.Cells & ASPOSE.Words 操纵Excel和Word文档的 .NET Core 实例
    ASP.NET Core 3.x 中间件流程与路由体系
    以正确的方式下载和配置 ASP.NET Core 官方源码
  • 原文地址:https://www.cnblogs.com/jinzesudawei/p/8526213.html
Copyright © 2011-2022 走看看