原創(chuàng)|使用教程|編輯:王香|2017-07-25 17:40:36.000|閱讀 1296 次
概述:Spire.Doc 是一個(gè)MS Word 組件,使用戶可以直接執(zhí)行各種Word文檔處理任務(wù),本教程講述了如何在C#,VB.NET創(chuàng)建、編輯和保存Word.
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
新創(chuàng)建的Word文檔可以寫入內(nèi)容并保存到指定的路徑。 本指南演示了在C#和VB.NET中創(chuàng)建,編寫和保存Word的解決方案。
Word的基本操作是生成,寫入內(nèi)容并保存。 首先,用戶可以創(chuàng)建一個(gè)空白的Word文檔,然后在本文檔中寫下要說的內(nèi)容,最后保存到指定的路徑。 本指南重點(diǎn)介紹如何通過Spire.Doc for .NET來創(chuàng)建,編寫和保存C#和VB.NET中的Word。 下面的截圖是通過以下步驟進(jìn)行編程后的結(jié)果。
創(chuàng)建Word
Spire.Doc for .NET提供了一個(gè)Document類,使開發(fā)人員能夠初始化一個(gè)新的Document實(shí)例。 這個(gè)實(shí)例是一個(gè)新的空白Word文檔。
[C#]
//Create New Word Document doc = new Document();
[VB.NET]
'Create New Word Dim doc As New Document()
在Word中寫入內(nèi)容
一般來說,內(nèi)容是寫在文件一節(jié)中的段落中。 因此,您需要首先通過調(diào)用Document.AddSection()方法添加一個(gè)新的部分,并通過調(diào)用Section.AddParagraph()在新的部分添加一個(gè)新的段落,之后,您可以通過調(diào)用Paragraph.AppendText(string text)方法在段落中寫入內(nèi)容。
[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
調(diào)用Document類的SaveToFile方法來保存寫入的Word,傳遞給此方法的參數(shù)是string fileName,如果要將擴(kuò)展名設(shè)置為.doc或.docx,則需要將其他參數(shù)FileFormat fileFormat傳遞給此方法。
[C#]
//Save Word doc.SaveToFile("OperateWord.docx", FileFormat.Docx);
[VB.NET]
'Save Word doc.SaveToFile("OperateWord.docx", FileFormat.Docx)
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn