C# 讀取txt檔案生成Word文件

iceblue發表於2022-01-28

本文將以C#程式程式碼為例介紹如何來讀取txt檔案中的內容,生成Word文件。在編輯程式碼前,可參考如下程式碼環境進行配置:

 

dll檔案安裝(3種方法)


 

1.通過 NuGet 安裝dll(2種方法)

  1.1 可以在Visual Studio中開啟“解決方案資源管理器”,滑鼠右鍵點選“引用”,“管理NuGet包”,然後搜尋“Free Spire.Doc”,點選“安裝”。等待程式安裝完成。

  1.2 將以下內容複製到PM控制檯安裝。

        Install-Package FreeSpire.Doc -Version 9.9.7

2.手動新增dll引用

可通過手動 下載包,然後解壓,找到BIN資料夾下的Spire.Doc.dll。然後在Visual Studio中開啟“解決方案資源管理器”,滑鼠右鍵點選“引用”,“新增引用”,將本地路徑BIN資料夾下的dll檔案新增引用至程式。

 

讀取txt生成Word


 

  • 通過StreamReader(Stream stream, Encoding encoding)構造方法讀取指定路徑下的txt檔案。
  • 通過Free Spire.Doc 提供的Paragraph.AppendText(string text)方法將讀取到的txt內容新增到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)
        {
            //例項化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

                //設定段落樣式,並應用到段落
                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())
            '例項化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
                '設定段落樣式,並應用到段落
                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

效果圖:

 

 

 

注意事項


 

程式碼中的txt檔案和生成的Word文件路徑為F:\VS2017Project\CreateWordDocument_Doc\CreateWordDocument_Doc\bin\Debug下,檔案路徑也可以自定義。

 

—End—

相關文章