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

作者:E-iceblue 时间:2022-08-06 19:03:22 

本文将以C#程序代码为例介绍如何来读取txt文件中的内容,生成Word文档。在编辑代码前,可参考如下代码环境进行配置:

Visual Studio 2017

.Net Framework 4.6.1

Free Spire.Doc for .NET

.txt文档

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

效果图:

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

注意事项

代码中的txt文件和生成的Word文档路径为F:\VS2017Project\CreateWordDocument_Doc\CreateWordDocument_Doc\bin\Debug下,文件路径也可以自定义。

来源:https://www.cnblogs.com/Yesi/p/15852088.html

标签:C#,txt,Word
0
投稿

猜你喜欢

  • Springboot过滤器禁止ip频繁访问功能实现

    2022-08-29 11:20:59
  • C#同步、异步远程下载文件实例

    2023-08-26 21:08:49
  • 图解红黑树及Java进行红黑二叉树遍历的方法

    2023-04-20 22:09:14
  • SpringBoot ApplicationContext接口深入分析

    2021-10-21 23:00:03
  • springboot @ConfigurationProperties和@PropertySource的区别

    2023-06-12 07:18:14
  • SpringBoot Admin 如何实现Actuator端点可视化监控

    2022-12-09 01:40:51
  • java多线程有序读取同一个文件

    2022-05-28 20:41:57
  • springboot调用支付宝第三方接口(沙箱环境)

    2023-11-25 06:12:08
  • spring boot配置读写分离的完整实现步骤

    2022-02-15 21:23:11
  • Springboot Vue可配置调度任务实现示例详解

    2023-11-09 03:33:19
  • Springboot Mybatis Plus自动生成工具类详解代码

    2022-09-17 12:01:57
  • 基于Tomcat7、Java、WebSocket的服务器推送聊天室实例

    2023-11-25 23:35:34
  • 开发工具EesyCode使用方法解析

    2023-06-27 13:50:46
  • C#中label内容显示不全、不完整的解决方法

    2022-09-13 02:43:46
  • 基于C#方法重载的总结详解

    2022-07-29 13:19:46
  • Spring BeanFactory和FactoryBean区别解析

    2023-09-18 15:38:28
  • Java设计模式之工厂模式

    2023-12-18 01:40:50
  • C#字符串内存分配与驻留池学习分享

    2022-07-02 12:11:29
  • C#计算两个文件的相对目录算法的实例代码

    2022-08-27 10:27:44
  • 浅析Mybatis 在CS程序中的应用

    2023-06-24 08:44:15
  • asp之家 软件编程 m.aspxhome.com