打造一個很簡單的文檔轉換器 - 使用組件 Spire.Office
【博主】反骨仔 【原文】http://www.cnblogs.com/liqingwen/p/6024827.html
序
之前,《開頭不講"Hello Word",讀盡詩書也枉然 : Word 操作組件介紹 - Spire.Doc》一文簡單介紹了 Spire.Doc 如何使用。這次我們來介紹如何打造一個簡單的文檔轉換器。
目錄
Spire.Office 介紹
關於 Spire.Office,它是一個專門為開發人員創建,讀取,寫入設計的庫,轉換和從打印 word 文檔文件。作為一個獨立的 .NET組件,它不需要在機器上安裝微軟的 Word 等辦公軟件。然而,它可以將微軟的“文檔創建功能”集成到任何開發人員的網絡應用程序中。它是一個可靠的 MS Word 的API,可以執行許多Word文檔處理任務。它支持 C #,VB.NET,ASP.NET 和 ASP.NET 的 MVC,以及支持Word 97-2003 / 2007 / 2010 / 2013 並能將它們轉換為常用的文件格式,如 XML,RTF,TXT,XPS,EPUB 等高質量轉換,反之亦然。它是一款來自 E-iceblue 公司開發的組件。
以下是摘取“慧都控件網”對該公司的簡單介紹。
E-iceblue 是一個 .NET、Silverlight 和 WPF 開發控件供應商。e-iceblue 的目標是為客戶提供高質量的控件去閱讀和寫作不同格式的文件。E-iceblue 的控件被大部分的世界 500 強企業廣泛使用。e-iceblue 的主要開發者在開發高性能、高質量的 .NET、Silverlight 和 WPF 控件技術方面有超過 10 年的經驗。每天,e-iceblue 產品幫助大量的來自超過 60 個國家的大型/小型公司的開發人員從更容易、更好、更快和更富有成效的開發和向顧客交付可靠的應用程序。
庫引用
我只是想打造 word 和 excel 轉換器,所以只在 Nuget 中安裝上圖中的 ~.Doc 和 ~.XLS。
界面預覽
我發現當 ~.Doc 和 ~.XLS 同時裝在一個類庫中的時候,在轉換部分類型時會出現異常,所以采取了分層的形式。
WordConverter 只引用 ~.Doc。
這是很普通的一款拖控件完成的轉換器。
代碼片段
將核心的轉換代碼提煉出來會發現,使用起來是比較簡單的。這里是 Word 轉換的代碼,Excel 可以依葫蘆畫瓢。
1 //創建文檔對象 2 var document = new Document(); 3 4 //加載文檔 5 document.LoadFromFile("包含路徑的文件名"); //例:document.SaveToFile("Sample.pdf", FileFormat.PDF); 6 //保存文件 7 document.SaveToFile("包含文件名的路徑", "想轉換的文檔格式類型"); 8 9 //打開文件,預覽操作 10 Process.Start("包含路徑的文件名");
這里的文檔格式類型的支持也是比較多的,FileFormat 枚舉。
1 public enum FileFormat 2 { 3 // 4 // 摘要: 5 // Microsoft Word 97 - 2003 Binary Document. 6 Doc = 0, 7 // 8 // 摘要: 9 // Microsoft Word 97 - 2003 Binary Document or Template. 10 Dot = 1, 11 // 12 // 摘要: 13 // Microsoft Word 2007 Document. 14 Docx = 2, 15 // 16 // 摘要: 17 // Microsoft Word 2010 Document 18 Docx2010 = 3, 19 // 20 // 摘要: 21 // Microsoft Word 2013 Document 22 Docx2013 = 4, 23 // 24 // 摘要: 25 // Microsoft Word 2007 Template format. 26 Dotx = 5, 27 // 28 // 摘要: 29 // Microsoft Word 2010 Template format. 30 Dotx2010 = 6, 31 // 32 // 摘要: 33 // Microsoft Word 2013 Template format. 34 Dotx2013 = 7, 35 // 36 // 摘要: 37 // Microsoft Word 2007 macro enabled file format. 38 Docm = 8, 39 // 40 // 摘要: 41 // Microsoft Word 2010 macro enabled file format. 42 Docm2010 = 9, 43 // 44 // 摘要: 45 // Microsoft Word 2013 macro enabled file format. 46 Docm2013 = 10, 47 // 48 // 摘要: 49 // Microsoft Word 2007 macro enabled template format. 50 Dotm = 11, 51 // 52 // 摘要: 53 // Microsoft Word 2010 macro enabled template format. 54 Dotm2010 = 12, 55 // 56 // 摘要: 57 // Microsoft Word 2013 macro enabled template format. 58 Dotm2013 = 13, 59 // 60 // 摘要: 61 // PDF format 62 PDF = 14, 63 // 64 // 摘要: 65 // Rtf format 66 Rtf = 15, 67 // 68 // 摘要: 69 // Xml file format. 70 Xml = 16, 71 // 72 // 摘要: 73 // Text file format. 74 Txt = 17, 75 // 76 // 摘要: 77 // Html format. 78 Html = 18, 79 // 80 // 摘要: 81 // XPS format 82 XPS = 19, 83 // 84 // 摘要: 85 // EPub format 86 EPub = 20, 87 // 88 // 摘要: 89 // WordprocessingML format 90 WordML = 21, 91 // 92 // 摘要: 93 // Word xml format. 94 WordXml = 22, 95 // 96 // 摘要: 97 // The document is in the Word 6 or Word 95 format. Spire.Doc does not currently 98 // support loading such documents. 99 DocPre97 = 23, 100 // 101 // 摘要: 102 // Instructs Spire.Doc to recognize the format automatically. 103 Auto = 24 104 }
只是一些新手入門的代碼,看起來沒什么好說的。
Demo 下載地址:http://git.oschina.net/liqingwen/OfficeConverter
傳送門
《開頭不講"Hello Word",讀盡詩書也枉然 : Word 操作組件介紹 - Spire.Doc》