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#]
[VB.NET]
在Word中写入内容
一般来说,内容是写在文件一节中的段落中。 因此,您需要首先通过调用Document.AddSection()方法添加一个新的部分,并通过调用Section.AddParagraph()在新的部分添加一个新的段落,之后,您可以通过调用Paragraph.AppendText(string text)方法在段落中写入内容。
[C#]
[VB.NET]
保存Word
调用Document类的SaveToFile方法来保存写入的Word,传递给此方法的参数是string fileName,如果要将扩展名设置为.doc或.docx,则需要将其他参数FileFormat fileFormat传递给此方法。
[C#]
[VB.NET]
慧都控件网
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)
慧都控件网
相关文章推荐
- Spire.Doc 教程:在C#,VB.NET创建、编辑和保存Word
- Word控件Spire.Doc 教程:用C#,VB.NET在Word中创建对书签的交叉引用
- Word控件Spire.Doc 教程:用C#,VB.NET在Word中创建对书签的交叉引用
- Spire.Doc 教程:从C#和VB.NET的word文档中删除形状
- Spire.Doc 教程:从C#和VB.NET的word文档中删除形状
- Spire.Doc 教程:如何在C#,VB.NET中为Word文档插入形状和形状组
- Word控件Spire.Doc 教程:在C#,VB.NET的Word中检索所有TextRanges样式名称
- Spire.Doc 教程:如何在C#,VB.NET中为Word文档插入形状和形状组
- Word组件 Spire.Doc 教程:如何在C#,VB.NET中将XML转换为PDF
- Word教程Spire.Doc 教程:在C#,VB.NET的Word中检索所有TextRanges样式名称
- Word组件 Spire.Doc 教程:如何在C#,VB.NET中将XML转换为PDF
- Spire.Doc 教程:在word文档中创建书签
- Word组件Spire.Doc 教程:在word文档中创建书签
- Word组件Spire.Doc 教程:如何在C#中设置段落前后的间距
- Spire.Email 教程:从C#,VB.NET中的PST文件获取文件夹信息
- Spire.Doc 教程:用C#识别Word中的合并字段名称
- Spire.Doc 教程:将word文档中的隐藏文本保存到PDF
- Word控件Spire.Doc 教程:如何在C#中设置word 脚注的位置和数字格式
- Word控件Spire.Doc 教程:如何在C#中设置word 脚注的位置和数字格式
- vb.net创建一个word文档并保存