新建立的Word文檔能夠寫入內容並保存到指定的路徑。 本指南演示了在C#和VB.NET中建立,編寫和保存Word的解決方案。html
Word的基本操做是生成,寫入內容並保存。 首先,用戶能夠建立一個空白的Word文檔,而後在本文檔中寫下要說的內容,最後保存到指定的路徑。 本指南重點介紹如何經過Spire.Doc for .NET來建立,編寫和保存C#和VB.NET中的Word。 下面的截圖是經過如下步驟進行編程後的結果。編程
建立Wordapp
Spire.Doc for .NET提供了一個Document類,使開發人員可以初始化一個新的Document實例。 這個實例是一個新的空白Word文檔。component
[C#]orm
//Create New Word Document doc = new Document();
[VB.NET]htm
'Create New Word Dim doc As New Document()
在Word中寫入內容ci
通常來講,內容是寫在文件一節中的段落中。 所以,您須要首先經過調用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]get
'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)