Word控件Spire.Doc 转换教程(一):在 C#、VB.NET 中将 Word Doc 转换为 XML

谈到 Office OpenXML,我们可能会想到 HTML。实际上,Office OpenXML 与 HTML 类似,都是基于标签的语言。Office OpenXML 和 HTML 之间的区别在于 Office OpenXML 使用的标记不是预定义的。如果我们在 Office OpenXML 中创建自己的标签,我们需要遵循一些规则。

首先,Office OpenXML 文档中只包含一个根元素。根元素通常被视为文档元素并出现在序言部分之后。此外,所有 Office OpenXML 元素都应包含结束标记。开始和结束标记都应该相同。此外,元素不能重叠。更重要的是,所有的属性值都必须使用引 ,我们不能在文本中使用一些特殊字符。遵循规则后,Office OpenXML 文档将被格式化。

使用 C# 和 VB.NET 通过 Spire.Doc 将 Doc 转换为 Office OpenXML

Spire.Doc ( Spire.Office ) 为您提供了一种将 Doc 转换为 Office OpenXML 的简单方法。这样,我们只需点击几下,就可以将现有的 Word doc 文件转换为 Office OpenXML 格式。现在,只需按照简单的步骤。

第 1 步:创建项目

下载 Spire.Doc并安装在系统上。通过 Visual Studio 创建一个项目并添加 Spire.Doc DLL 作为参考。

注意:请确保 Spire.Doc 和 Visual Studio 已正确安装在系统上

第 2 步:加载 Word 文档文件

加载我们需要转换为 Office OpenXML 格式的本地 Word 文档文件。下面的代码可以帮助我们加载它:

Document document = new Document();document.LoadFromFile(@"D:Sample.doc");
第 3 步:将 Doc 转换为 Office OpenXML

Spire.Doc 支持将 Word Doc 文件转换为大多数流行的文件格式,例如 PDF、HTML、Office OpenXML、EPub、RTF、Dot、Text 等。现在,使用下面的代码将 Word 转换为 Office OpenXML。

document.SaveToFile("Sample.xml", FileFormat.Xml);
第 4 步:完整代码

现在,将完整代码写入您的项目,然后按 F5 启动程序。

[C#]
using System;using System.Windows.Forms;using Spire.Doc;using Spire.Doc.Documents;namespace to XML{public partial class Form1 : Form{public Form1(){InitializeComponent();}private void button1_Click(object sender, EventArgs e){//Create word documentDocument document = new Document();document.LoadFromFile(@"D:Sample.doc");//Save doc file.document.SaveToFile("Sample.xml", FileFormat.Xml);//Launching the MS Word file.WordDocViewer("Sample.xml");}private void WordDocViewer(string fileName){try{System.Diagnostics.Process.Start(fileName);}catch { }}}}
[VB.NET]
Imports SystemImports System.Windows.FormsImports Spire.DocImports Spire.Doc.DocumentsNamespace to XMLPartial Public Class Form1Inherits FormPublic Sub New()InitializeComponent()End SubPrivate Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs)'Create word documentDim document As New Document()document.LoadFromFile("D:Sample.doc")'Save doc file.document.SaveToFile("Sample.xml", FileFormat.Xml);'Launching the MS Word file.WordDocViewer("Sample.xml")End SubPrivate Sub WordDocViewer(ByVal fileName As String)TrySystem.Diagnostics.Process.Start(fileName)CatchEnd TryEnd SubEnd ClassEnd Namespace

运行演示后,您可能会在浏览器上找到一个 Office OpenXML 文档:


欢迎下载|体验E-iceblue产品 
如需获取更多产品信息,请您咨询在线客服

标签:

声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2022年1月1日
下一篇 2022年1月1日

相关推荐

发表回复

登录后才能评论