新建立的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)