C#實(shí)現(xiàn)讀取txt文件生成Word文檔
本文將以C#程序代碼為例介紹如何來讀取txt文件中的內(nèi)容,生成Word文檔。在編輯代碼前,可參考如下代碼環(huán)境進(jìn)行配置:
Visual Studio 2017
.Net Framework 4.6.1
.txt文檔
dll文件安裝(3種方法)
1.通過NuGet安裝dll(2種方法)
1.1 可以在Visual Studio中打開“解決方案資源管理器”,鼠標(biāo)右鍵點(diǎn)擊“引用”,“管理NuGet包”,然后搜索“Free Spire.Doc”,點(diǎn)擊“安裝”。等待程序安裝完成。
1.2 將以下內(nèi)容復(fù)制到PM控制臺安裝。
Install-Package FreeSpire.Doc -Version 9.9.7
2.手動添加dll引用
可通過手動下載包,然后解壓,找到BIN文件夾下的Spire.Doc.dll。然后在Visual Studio中打開“解決方案資源管理器”,鼠標(biāo)右鍵點(diǎn)擊“引用”,“添加引用”,將本地路徑BIN文件夾下的dll文件添加引用至程序。
讀取txt生成Word
通過StreamReader(Stream stream, Encoding encoding)構(gòu)造方法讀取指定路徑下的txt文件。
通過Free Spire.Doc 提供的Paragraph.AppendText(string text)方法將讀取到的txt內(nèi)容添加到Word段落。
最后,通過Document.SaveToFile(string fileName, FileFormat fileFormat)方法保存為Word,并指定保存路徑。
C#
using Spire.Doc; using Spire.Doc.Documents; using System.Drawing; using System.IO; using System.Text; namespace CreateWordDocument_Doc { class Program { static void Main(string[] args) { //實(shí)例化Document類的對象,并添加section和paragraph Document doc = new Document(); Section section = doc.AddSection(); Paragraph paragraph = section.AddParagraph(); //讀取txt文件 StreamReader sr = new StreamReader("test.txt", Encoding.Default); string line; while ((line = sr.ReadLine()) != null) { paragraph.AppendText(line);//在段落中寫入txt //設(shè)置段落樣式,并應(yīng)用到段落 ParagraphStyle style1 = new ParagraphStyle(doc); style1.Name = "titleStyle"; style1.CharacterFormat.Bold = true; style1.CharacterFormat.TextColor = Color.Purple; style1.CharacterFormat.FontName = "宋體"; style1.CharacterFormat.FontSize = 12; doc.Styles.Add(style1); paragraph.ApplyStyle("titleStyle"); } //保存為docx格式的Word doc.SaveToFile("addTxttoWord.docx", FileFormat.Docx2013); System.Diagnostics.Process.Start("addTxttoWord.docx"); } } }
VB,NET
Imports Spire.Doc Imports Spire.Doc.Documents Imports System.Drawing Imports System.IO Imports System.Text Namespace CreateWordDocument_Doc Class Program Private Shared Sub Main(args As String()) '實(shí)例化Document類的對象,并添加section和paragraph Dim doc As New Document() Dim section As Section = doc.AddSection() Dim paragraph As Paragraph = section.AddParagraph() '讀取txt文件 Dim sr As New StreamReader("test.txt", Encoding.[Default]) Dim line As String While (InlineAssignHelper(line, sr.ReadLine())) IsNot Nothing paragraph.AppendText(line) '在段落中寫入txt '設(shè)置段落樣式,并應(yīng)用到段落 Dim style1 As New ParagraphStyle(doc) style1.Name = "titleStyle" style1.CharacterFormat.Bold = True style1.CharacterFormat.TextColor = Color.Purple style1.CharacterFormat.FontName = "宋體" style1.CharacterFormat.FontSize = 12 doc.Styles.Add(style1) paragraph.ApplyStyle("titleStyle") End While '保存為docx格式的Word doc.SaveToFile("addTxttoWord.docx", FileFormat.Docx2013) System.Diagnostics.Process.Start("addTxttoWord.docx") End Sub Private Shared Function InlineAssignHelper(Of T)(ByRef target As T, value As T) As T target = value Return value End Function End Class End Namespace
效果圖:
注意事項(xiàng)
代碼中的txt文件和生成的Word文檔路徑為F:\VS2017Project\CreateWordDocument_Doc\CreateWordDocument_Doc\bin\Debug下,文件路徑也可以自定義。
總結(jié)
到此這篇關(guān)于C#實(shí)現(xiàn)讀取txt文件生成Word文檔的文章就介紹到這了,更多相關(guān)C#讀取txt生成Word內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#在運(yùn)行時(shí)動態(tài)創(chuàng)建類型的實(shí)現(xiàn)方法
這篇文章主要介紹了C#在運(yùn)行時(shí)動態(tài)創(chuàng)建類型的實(shí)現(xiàn)方法,主要通過動態(tài)生成C#代碼再編譯成程序集來實(shí)現(xiàn)動態(tài)創(chuàng)建類型的,需要的朋友可以參考下2014-09-09C#實(shí)現(xiàn)對用戶輸入數(shù)據(jù)進(jìn)行校驗(yàn)的類實(shí)例
這篇文章主要介紹了C#實(shí)現(xiàn)對用戶輸入數(shù)據(jù)進(jìn)行校驗(yàn)的類,實(shí)例分析了C#針對各種用戶輸入數(shù)據(jù)的常用校驗(yàn)技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-03-03