您的位置:首页 > 编程语言 > VB

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

2017-07-26 09:58 225 查看
新创建的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,
c659
" & "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)


慧都控件网
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: