[.NET] 打造一个很简单的文档转换器 - 使用组件 Spire.Office

来源:互联网 发布:linux test -e 编辑:程序博客网 时间:2024/05/01 23:25

打造一个很简单的文档转换器 - 使用组件 Spire.Office

目录

  • Spire.Office 介绍
  • 库引用
  • 界面预览
  • 代码片段

 

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。

 

  这是很普通的一款拖控件完成的转换器。

 

代码片段

  将核心的转换代码提炼出来会发现,使用起来是比较简单的。

复制代码
 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 = 24104     }
复制代码

 

 

 

  只是一些新手入门的代码,看起来没什么好说的。

0 0