概述
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); } }
生成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); } }
讀取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); } }
生成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); } }
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); }
以上代碼下載地址:NiceComponents · Bruce/Learn WPF - 碼雲 - 開源中國 (gitee.com)
本文只是演示了一些基本應用,表格、圖片等都沒有涉及,主要是官方文檔已經非常詳細了,更多高級功能請參考幫助文檔。