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

末蓝、 2023-10-11 17:01 136阅读 0赞

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

  1. using Spire.Doc;
  2. using Spire.Doc.Documents;
  3. using System.Drawing;
  4. using System.IO;
  5. using System.Text;
  6. namespace CreateWordDocument_Doc
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. //实例化Document类的对象,并添加section和paragraph
  13. Document doc = new Document();
  14. Section section = doc.AddSection();
  15. Paragraph paragraph = section.AddParagraph();
  16. //读取txt文件
  17. StreamReader sr = new StreamReader("test.txt", Encoding.Default);
  18. string line;
  19. while ((line = sr.ReadLine()) != null)
  20. {
  21. paragraph.AppendText(line);//在段落中写入txt
  22. //设置段落样式,并应用到段落
  23. ParagraphStyle style1 = new ParagraphStyle(doc);
  24. style1.Name = "titleStyle";
  25. style1.CharacterFormat.Bold = true;
  26. style1.CharacterFormat.TextColor = Color.Purple;
  27. style1.CharacterFormat.FontName = "宋体";
  28. style1.CharacterFormat.FontSize = 12;
  29. doc.Styles.Add(style1);
  30. paragraph.ApplyStyle("titleStyle");
  31. }
  32. //保存为docx格式的Word
  33. doc.SaveToFile("addTxttoWord.docx", FileFormat.Docx2013);
  34. System.Diagnostics.Process.Start("addTxttoWord.docx");
  35. }
  36. }
  37. }

VB.NET

  1. Imports Spire.Doc
  2. Imports Spire.Doc.Documents
  3. Imports System.Drawing
  4. Imports System.IO
  5. Imports System.Text
  6. Namespace CreateWordDocument_Doc
  7. Class Program
  8. Private Shared Sub Main(args As String())
  9. '实例化Document类的对象,并添加section和paragraph
  10. Dim doc As New Document()
  11. Dim section As Section = doc.AddSection()
  12. Dim paragraph As Paragraph = section.AddParagraph()
  13. '读取txt文件
  14. Dim sr As New StreamReader("test.txt", Encoding.[Default])
  15. Dim line As String
  16. While (InlineAssignHelper(line, sr.ReadLine())) IsNot Nothing
  17. paragraph.AppendText(line)
  18. '在段落中写入txt
  19. '设置段落样式,并应用到段落
  20. Dim style1 As New ParagraphStyle(doc)
  21. style1.Name = "titleStyle"
  22. style1.CharacterFormat.Bold = True
  23. style1.CharacterFormat.TextColor = Color.Purple
  24. style1.CharacterFormat.FontName = "宋体"
  25. style1.CharacterFormat.FontSize = 12
  26. doc.Styles.Add(style1)
  27. paragraph.ApplyStyle("titleStyle")
  28. End While
  29. '保存为docx格式的Word
  30. doc.SaveToFile("addTxttoWord.docx", FileFormat.Docx2013)
  31. System.Diagnostics.Process.Start("addTxttoWord.docx")
  32. End Sub
  33. Private Shared Function InlineAssignHelper(Of T)(ByRef target As T, value As T) As T
  34. target = value
  35. Return value
  36. End Function
  37. End Class
  38. End Namespace

watermark_type_d3F5LXplbmhlaQ_shadow_50_text_Q1NETiBARWljZWJsdWU_size_20_color_FFFFFF_t_70_g_se_x_16

#

注意事项


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

—End—

发表评论

表情:
评论列表 (有 0 条评论,136人围观)

还没有评论,来说两句吧...

相关阅读