原創|使用教程|編輯:王香|2017-07-19 14:28:01.000|閱讀 859 次
概述:Spire.Doc 是一個MS Word組件,使用戶可以直接執行各種Word文檔處理任務,本教程講述了如何在C#,VB.NET中將Word文檔轉換為XML。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
關于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等?,F在,使用以下代碼將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 System Imports System.Windows.Forms Imports Spire.Doc Imports Spire.Doc.Documents Namespace 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文檔:
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn