原創|使用教程|編輯:王香|2017-07-25 17:40:36.000|閱讀 1296 次
概述:Spire.Doc 是一個MS Word 組件,使用戶可以直接執行各種Word文檔處理任務,本教程講述了如何在C#,VB.NET創建、編輯和保存Word.
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
新創建的Word文檔可以寫入內容并保存到指定的路徑。 本指南演示了在C#和VB.NET中創建,編寫和保存Word的解決方案。
Word的基本操作是生成,寫入內容并保存。 首先,用戶可以創建一個空白的Word文檔,然后在本文檔中寫下要說的內容,最后保存到指定的路徑。 本指南重點介紹如何通過Spire.Doc for .NET來創建,編寫和保存C#和VB.NET中的Word。 下面的截圖是通過以下步驟進行編程后的結果。
創建Word
Spire.Doc for .NET提供了一個Document類,使開發人員能夠初始化一個新的Document實例。 這個實例是一個新的空白Word文檔。
[C#]
//Create New Word Document doc = new Document();
[VB.NET]
'Create New Word Dim doc As New Document()
在Word中寫入內容
一般來說,內容是寫在文件一節中的段落中。 因此,您需要首先通過調用Document.AddSection()方法添加一個新的部分,并通過調用Section.AddParagraph()在新的部分添加一個新的段落,之后,您可以通過調用Paragraph.AppendText(string text)方法在段落中寫入內容。
[C#]
//Add Section Section section = doc.AddSection(); //Add Paragraph Paragraph Para = section.AddParagraph(); //Append Text Para.AppendText("Spire.Doc for .NET, a professional .NET Word component, " +"enables developers to perform a large range of tasks on Word document(from Version Word97-2003 to Word 2010) " +"for .NET in C# and VB.NET." +"This libray is specially designed for .NET developers to help them" +"to create any WinForm and ASP.NET Web applications to create, open, write, edit, save and convert" +"Word document without Microsoft Office and any other third-party tools installed on system.");
[VB.NET]
'Add Section Dim section As Section = doc.AddSection() 'Add Paragraph Dim Para As Paragraph = section.AddParagraph() 'Append Text Para.AppendText("Spire.Doc for .NET, a professional .NET Word component, " & "enables developers to perform a large range of tasks on Word document(from Version Word97-2003 to Word 2010) " & "for .NET in C# and Visual Basic." & "This libray is specially designed for .NET developers to help them" & "to create any WinForm and ASP.NET Web applications to create, open, write, edit, save and convert" & "Word document without Microsoft Office and any other third-party tools installed on system.")
保存Word
調用Document類的SaveToFile方法來保存寫入的Word,傳遞給此方法的參數是string fileName,如果要將擴展名設置為.doc或.docx,則需要將其他參數FileFormat fileFormat傳遞給此方法。
[C#]
//Save Word doc.SaveToFile("OperateWord.docx", FileFormat.Docx);
[VB.NET]
'Save Word doc.SaveToFile("OperateWord.docx", FileFormat.Docx)
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn