-
C# 读取txt文件生成Word文档
本文将以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
效果图:
注意事项
代码中的txt文件和生成的Word文档路径为F:\VS2017Project\CreateWordDocument_Doc\CreateWordDocument_Doc\bin\Debug下,文件路径也可以自定义。
出处:https://www.cnblogs.com/Yesi/p/15852088.html
栏目列表
最新更新
Python获取微信好友数据
Python 的排序方法 sort 和 sorted 的区别
WinForm轻松实现自定义分页 (转载)
Mac系统下使用brew搭建PHP(LNMP/LAMP)开发环境
在win系统安装配置 Memcached for PHP 5.3 图文
箱图在数据预处理中的应用
Python WEB开发:用Tornado框架制作简易【表
自从学会了用python解析视频,都不用去找
Python代码阅读(第50篇):对列表间隔取
Python爬虫+数据分析+可视化展示,分析《
MongoDB常用命令(2)
MongoDB基本介绍与安装(1)
SQLServer触发器调用JavaWeb接口
SQL Server索引的原理深入解析
SqlServer2016模糊匹配的三种方式及效率问题
SQL中Truncate的用法
sqlserver 多表关联时在where语句中慎用tri
链接服务器读取Mysql---出现消息 7347,级别
SQL Server解惑——为什么你拼接的SQL语句换
MySQL视图了解一下
laf.js - 开源的云开发框架(README.md)
javascript创建对象
Node.js 源码分析 - 从 main 函数开始
Node.js 源码分析 - 原生模块(C++模块)的注册
ECharts图标中用的js相关的处理方法
GoJS 使用笔记
单元测试 - 测试场景记录
Node.js 源码分析 - 加载 js 文件
ES6入门
聊聊动效降级