新创建的Word文档可以写入内容并保存到指定的路径。 本指南演示了在C#和VB.NET中创建,编写和保存Word的解决方案。
创建Word
Spire.Doc for .NET提供了一个Document类,使开发人员能够初始化一个新的Document实例。 这个实例是一个新的空白Word文档。
[C#]
//Create New WordDocument doc = new Document();
[VB.NET]
'Create New Word Dim doc As New Document()
在Word中写入内容
一般来说,内容是写在文件一节中的段落中。 因此,您需要首先通过调用Document.AddSection()方法添加一个新的部分,并通过调用Section.AddParagraph()在新的部分添加一个新的段落,之后,您可以通过调用Paragraph.AppendText(string text)方法在段落中写入内容。
[C#]
//Add SectionSection section = doc.AddSection();//Add ParagraphParagraph Para = section.AddParagraph();//Append TextPara.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 SectionDim section As Section = doc.AddSection()'Add ParagraphDim Para As Paragraph = section.AddParagraph()'Append TextPara.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 Worddoc.SaveToFile("OperateWord.docx", FileFormat.Docx);
[VB.NET]
'Save Worddoc.SaveToFile("OperateWord.docx", FileFormat.Docx)
控件
标签:文档管理word文档处理
声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!