• WPF优秀组件推荐之FreeSpire


    概述

    Spire是一套可以轻松处理Word、Excel和PDF的商业组件,需要收费,但是他有一套对应的免费组件FreeSpire可以使用,免费组件在功能上有一些限制(比如:excel的sheet数量不能超过30),对于普通应用来说大部分场景下都可以适用了。

    中文帮助文档:帮助文档 | 全面丰富的在线文档,助您快速了解如何使用产品 

    本文代码基于Stylet开发,如果您还不了解Stylet,请参阅:

    WPF优秀组件推荐之Stylet(一) - seabluescn - 博客园 (cnblogs.com)

    WPF优秀组件推荐之Stylet(二) - seabluescn - 博客园 (cnblogs.com)

    环境安装

    在Nuget中搜索:FreeSpire

    如果你只需要处理Excel或Word等,可以下载对应的包,怕麻烦可以下一个FreeSpire.Office的总包。(建议下载FreeSpire.Office,虽然文件多一些,但后期功能升级不需要再加组件,也不会有不同组件版本之间不兼容的问题)

    生成Word文档

            public void SaveWord()
            {
                SaveFileDialog fileDialog = new SaveFileDialog()
                {
                    Filter = "Word File(*.docx)|*.docx",
                    FileName = "Report01" + ".docx",
                };
    
                if (fileDialog.ShowDialog() == true)
                {
                    Document document = new Document();
                    Section s = document.AddSection();             
                    Paragraph para1 = s.AddParagraph();
                    para1.AppendText("欢迎使用Spire.Doc");
    
                    document.SaveToFile(fileDialog.FileName, Spire.Doc.FileFormat.Docx);
                    Process.Start(fileDialog.FileName);
                }
            }
    View Code

      

    生成Excel文档

            public void SaveExcel()
            {
                SaveFileDialog fileDialog = new SaveFileDialog()
                {
                    Filter = "Word File(*.xlsx)|*.xlsx",
                    FileName = "Report01" + ".xlsx",
                };
    
                if (fileDialog.ShowDialog() == true)
                {
                    Workbook workbook = new Workbook();
                    Worksheet sheet = workbook.Worksheets[0];
    
                    sheet.Range[1, 1].Text = "步骤";
                    sheet.Range[1, 2].Text = "时间";               
    
                    int row = 2;
                    for (int i = 0; i <10; i++)
                    {                   
                        sheet.Range[row, 1].Text = i.ToString();
                        sheet.Range[row, 2].Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); 
                        row++;
                    }
    
                    sheet.Range[row + 2, 1].Text = "报告时间:";
                    sheet.Range[row + 2, 2].Text = $"2022-02-02 11:11:11";
                  
                    workbook.SaveToFile(fileDialog.FileName, ExcelVersion.Version2010);
                    Process.Start(fileDialog.FileName);
                }
            }
    View Code

      

    读取Word模板

    生成Word文档时,格式其实很难控制,有一个简单的办法就是先创建一个模板格式文件,动态的内容先用特殊的占位字符串,然后程序再把相应的占位字符串给替换掉,这样文件的样式就可以非常容易调整和修改,客户有什么特殊需求还能直接修改模板,都不用改代码。

     Code:

            public void LoadWord()
            {
                SaveFileDialog fileDialog = new SaveFileDialog()
                {
                    Filter = "Word File(*.docx)|*.docx",
                    FileName = "Report02" + ".docx",
                };
    
                if (fileDialog.ShowDialog() == true)
                {
                    Document document = new Document();
                    document.LoadFromFile(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Template", "WordTemplate.docx"));
    
                    document.Replace("<$ReportTitle>", "报表标题", false, true);
                    document.Replace("<$CompanyName>", "公司名称", false, true);
    
                    document.SaveToFile(fileDialog.FileName, Spire.Doc.FileFormat.Docx);
                    Process.Start(fileDialog.FileName);
                }
            }
    View Code

    生成Pdf文档

            public void SavePdf()
            {
                SaveFileDialog fileDialog = new SaveFileDialog()
                {
                    Filter = "Word File(*.pdf)|*.pdf",
                    FileName = "Report01" + ".pdf",
                };
    
                if (fileDialog.ShowDialog() == true)
                {
                    //初始化一个PdfDocument实例
                    PdfDocument document = new PdfDocument();
    
                    //设置边距
                    PdfUnitConvertor unitCvtr = new PdfUnitConvertor();
                    PdfMargins margins = new PdfMargins();
                    margins.Top = unitCvtr.ConvertUnits(2.54f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
                    margins.Bottom = margins.Top;
                    margins.Left = unitCvtr.ConvertUnits(3.17f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
                    margins.Right = margins.Left;
    
                    //添加新页
                    PdfPageBase page = document.Pages.Add(PdfPageSize.A4, margins);
    
                    //自定义PdfTrueTypeFont、PdfPen实例
                    PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("宋体", 11f), true);
                    PdfPen pen = new PdfPen(Color.Black);
    
                    //使用DrawString方法在指定位置写入文本
                    string text = "我的第一个C# PDF文档";
                    page.Canvas.DrawString(text, font, pen, 100, 50);
    
                    //保存文档
                    document.SaveToFile(fileDialog.FileName);
                    Process.Start(fileDialog.FileName);
                }
            }
    View Code

      

    Word转换为PDF

    Pdf的生成是比较麻烦的,更像是绘图操作,如果客户一定要Pdf格式报表,我一般先生成一个Word的临时文件,然后再转成pdf,当然Word的生成仍可以采用模板的方法。 

            public void WordToPdf()
            {
                var WordFilePath = @"E:\Report02.docx";
                var PdfFilePath = @"E:\Report02.pdf";
    
                Document document = new Document();
                document.LoadFromFile(WordFilePath);
                document.SaveToFile(PdfFilePath, Spire.Doc.FileFormat.PDF);
                Process.Start(PdfFilePath);
            }
    View Code

      

    以上代码下载地址:NiceComponents · Bruce/Learn WPF - 码云 - 开源中国 (gitee.com)

    本文只是演示了一些基本应用,表格、图片等都没有涉及,主要是官方文档已经非常详细了,更多高级功能请参考帮助文档。

  • 相关阅读:
    JQuery实现1024小游戏
    Windows Server2008 R2安装wampserver缺少api-ms-win-crt-runtime-l1-1-0.dll解决方案
    ASP.NET MVC 邮件发送的功能(微软邮箱发送)。
    浅谈撞库防御策略
    极验高并发验证服务背后的技术实现
    2015年国内数据安全事件盘点
    转载——验证码的昨天、今天和明天
    转载——最近百度云盘不提供搜索,闲来无事,玩玩python爬虫,爬一下百度云盘的资源
    SQL 查询语句
    SQL Server 目录
  • 原文地址:https://www.cnblogs.com/seabluescn/p/15981104.html
Copyright © 2020-2023  润新知