Spire.Doc 教程:在C#,VB.NET创建、编辑和保存Word

来源:互联网 发布:武汉网络施工队 编辑:程序博客网 时间:2024/06/05 07:00

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

慧都控件网

阅读全文
0 0
原创粉丝点击