Spire.Doc 教程:如何在C#,VB.NET中将Word文档转换为XML

Spire.Doc 是一个MS Word组件,使用户可以直接执行各种Word文档处理任务,本教程讲述了如何在C#,VB.NET中将Word文档转换为XML。

关于Office OpenXML的基本知识

在谈论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文档转换为Office OpenXML格式,操作如下。

Step 1:创建项目

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

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

Step 2: 加载Word文件文件

加载我们需要转换为Office OpenXML格式的本地Word文档。

Document document = new Document();document.LoadFromFile(@"D:Sample.doc");

Step 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);

Step 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 document            Document 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 XML    Partial Public Class Form1        Inherits Form        Public Sub New()            InitializeComponent()        End Sub        Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs)            'Create word document            Dim 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 Sub        Private Sub WordDocViewer(ByVal fileName As String)            Try                System.Diagnostics.Process.Start(fileName)            Catch            End Try        End Sub    End Class

运行演示后,您可能会在浏览器中发现Office OpenXML文档:

图片1

控件

标签:文档管理word文档处理

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

上一篇 2017年6月16日
下一篇 2017年6月16日

相关推荐

发表回复

登录后才能评论