惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

宝玉的分享
宝玉的分享
NISL@THU
NISL@THU
E
Exploit-DB.com RSS Feed
L
LINUX DO - 热门话题
L
Lohrmann on Cybersecurity
K
Kaspersky official blog
Project Zero
Project Zero
Cisco Talos Blog
Cisco Talos Blog
T
The Exploit Database - CXSecurity.com
P
Palo Alto Networks Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
T
Threatpost
S
Schneier on Security
G
GRAHAM CLULEY
The Hacker News
The Hacker News
T
Threat Research - Cisco Blogs
Scott Helme
Scott Helme
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
P
Privacy & Cybersecurity Law Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
Cyberwarzone
Cyberwarzone
C
CERT Recently Published Vulnerability Notes
T
Tor Project blog
AWS News Blog
AWS News Blog
Simon Willison's Weblog
Simon Willison's Weblog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
爱范儿
爱范儿
P
Privacy International News Feed
云风的 BLOG
云风的 BLOG
P
Proofpoint News Feed
S
Securelist
G
Google Developers Blog
The Last Watchdog
The Last Watchdog
Google Online Security Blog
Google Online Security Blog
美团技术团队
F
Fortinet All Blogs
小众软件
小众软件
Recorded Future
Recorded Future
V
Visual Studio Blog
B
Blog RSS Feed
H
Help Net Security
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Google DeepMind News
Google DeepMind News
Blog — PlanetScale
Blog — PlanetScale
博客园 - 聂微东
Stack Overflow Blog
Stack Overflow Blog
Martin Fowler
Martin Fowler
Latest news
Latest news
Spread Privacy
Spread Privacy
H
Heimdal Security Blog

博客园 - seabluescn

WPF开发快速入门【8】WPF进行简单的3D开发 WPF开发快速入门【7】WPF的拖放功能(Drag and Drop) WPF开发快速入门【6】下拉框与枚举类型 WPF开发快速入门【5】DataGrid的使用 WPF开发快速入门【4】自定义控件与用户控件 WPF开发快速入门【3】WPF的基本特性(附加属性) WPF开发快速入门【2】WPF的基本特性(Style、Trigger、Template) WPF开发快速入门【1】WPF的布局 WPF开发快速入门【0】前言与目录 WPF优秀组件推荐之LiveCharts WPF优秀组件推荐之MahApps WPF优秀组件推荐之Stylet(二) WPF优秀组件推荐之Stylet(一) TensorFlow.NET机器学习入门【9】后记 TensorFlow.NET机器学习入门【8】采用GPU进行学习 TensorFlow.NET机器学习入门【7】采用卷积神经网络(CNN)处理Fashion-MNIST TensorFlow.NET机器学习入门【6】采用神经网络处理Fashion-MNIST TensorFlow.NET机器学习入门【5】采用神经网络实现手写数字识别(MNIST) TensorFlow.NET机器学习入门【4】采用神经网络处理分类问题
WPF优秀组件推荐之FreeSpire
seabluescn · 2022-03-09 · via 博客园 - seabluescn

概述

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)

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