最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#實(shí)現(xiàn)讀取txt文件生成Word文檔

 更新時(shí)間:2022年01月28日 10:52:56   作者:E-iceblue  
大家好,本篇文章主要講的是C#實(shí)現(xiàn)讀取txt文件生成Word文檔,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下

本文將以C#程序代碼為例介紹如何來(lái)讀取txt文件中的內(nèi)容,生成Word文檔。在編輯代碼前,可參考如下代碼環(huán)境進(jìn)行配置:

Visual Studio 2017

.Net Framework 4.6.1

Free Spire.Doc for .NET

.txt文檔

dll文件安裝(3種方法)

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

1.1 可以在Visual Studio中打開“解決方案資源管理器”,鼠標(biāo)右鍵點(diǎn)擊“引用”,“管理NuGet包”,然后搜索“Free Spire.Doc”,點(diǎn)擊“安裝”。等待程序安裝完成。

1.2 將以下內(nèi)容復(fù)制到PM控制臺(tái)安裝。

Install-Package FreeSpire.Doc -Version 9.9.7

2.手動(dòng)添加dll引用

可通過(guò)手動(dòng)下載包,然后解壓,找到BIN文件夾下的Spire.Doc.dll。然后在Visual Studio中打開“解決方案資源管理器”,鼠標(biāo)右鍵點(diǎn)擊“引用”,“添加引用”,將本地路徑BIN文件夾下的dll文件添加引用至程序。

讀取txt生成Word

通過(guò)StreamReader(Stream stream, Encoding encoding)構(gòu)造方法讀取指定路徑下的txt文件。

通過(guò)Free Spire.Doc 提供的Paragraph.AppendText(string text)方法將讀取到的txt內(nèi)容添加到Word段落。

最后,通過(guò)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類的對(duì)象,并添加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類的對(duì)象,并添加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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

重庆市| 兴国县| 绥滨县| 华池县| 嵊州市| 连江县| 华安县| 安宁市| 泸水县| 汕尾市| 屏边| 琼海市| 迁安市| 西丰县| 昂仁县| 上思县| 盈江县| 双柏县| 南郑县| 塔城市| 旅游| 眉山市| 汉源县| 桐柏县| 蓬莱市| 健康| 香港| 安岳县| 遂平县| 喀喇沁旗| 永城市| 玛沁县| 寻乌县| 绥宁县| 白银市| 阳泉市| 揭东县| 宁阳县| 宝鸡市| 清水县| 那曲县|