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

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

慧都控件网

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值