C# 读取txt文件生成Word文档


本文将以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—

引用来源:https://www.cnblogs.com/Yesi/archive/2022/01/28/15852088.html

版权声明:本文为YES开发框架网发布内容,转载请附上原文出处连接
管理员
上一篇:一劳永逸,解决.NET发布云服务器的时区问题
下一篇:SourceGenerator的应用: .Net多进程开发库 - Juxtapose
评论列表

发表评论

评论内容
昵称:
关联文章

C# 读取txt文件生成Word文档
C# txt文本文件的读取与写入,
C# / VB.NET 在Word中嵌入多媒体(视频、音频)文件
C# 解析加载读取XML文件的正确姿势
bat生成文件夹中文件列表输出到txt
文件或目录损坏且无法读取
C#读取被进程占用的文件,只读模式打开文件
C# 中使用JavaScript给PDF文档设置过期时间
C# 在PDF文档中应用多种不同字体
C#根据文件流判断文件类型
Python读取文件 发生异常: UnicodeDecodeError 'gbk' codec can't decode byte
C#操作 配置文件*.ini
word页眉表格多一个空行
C# 利用 SharpZipLib 对多个文本字符串进行多文件打包为RAR或ZIP并进行下载
ASP.NET Core MVC 在过滤器ActionFilter中保存页面的生成的html静态页面文件
Python读取excel xlrd读取xlsx报错:Excel xlsx file; not supported
C# winform选择dll文件
C# ThoughtWorks.QRCode 二维码生成和解析
.NET Core生成后没有Nuget的dll文件
C#错误异常日志记录到文件

联系我们
联系电话:15090125178(微信同号)
电子邮箱:garson_zhang@163.com
站长微信二维码
微信二维码