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
效果图:
注意事项
代码中的txt文件和生成的Word文档路径为F:\VS2017Project\CreateWordDocument_Doc\CreateWordDocument_Doc\bin\Debug下,文件路径也可以自定义。
来源:https://www.cnblogs.com/Yesi/p/15852088.html


猜你喜欢
- Radiobutton是一种单选按钮,是由于RadioGroup管理下的一组按钮,所以一旦其中的一个button选中,再点击,就不能取消,想
- Lombok中@Builder用法1、建造者模式简介:Builder 使用创建者模式又叫建造者模式。简单来说,就是一步步创建一个对象,它对用
- mysql默认的隔离级别是repeatable_read根据spring文档,数据库一共有4个隔离级别read_uncommitread_c
- 1、二维数组对于一维数组,int arr[10]; arr是数组名,也是首元素的地址,&arr是数组的地址,那么对于二维数组 int
- 在HotSpot虚拟机中,对象在内存中存储的布局可以分为三块区域:对象头(Header)、实例数据(Instance Data)和对齐填充(
- 在Material Design里,CoordinatorLayout通常用来作为顶层视图,来协调处理各个子View之间的动作,从而实现各种
- 关于Path之前写的也很多了,例如path绘制线,path绘制一阶,二阶和三阶贝塞尔路径,这些都是path的基本用法。今天我要带大家看的是P
- 一、Filefile是文件和目录路径名的抽象表示1.1 File的用法用法:File file = new File("路径名&q
- gateway、webflux、reactor-netty请求日志输出场景在使用spring cloud gateway时想要输出请求日志,
- 解锁、唤醒屏幕用到KeyguardManager,KeyguardLock,PowerManager,PowerManager.WakeLo
- 1、使用第三方类库 HtmlAgilityPack官方网址:https://html-agility-pack.net/?z=codeple
- 在做商城的项目中,有这么个需求,就是一个产品下有两个价格,一个是市场价,一个是销售价,这时要把市场价添加个删除线;刚开始遇到这个时,在网上找
- 难点是泛型如何转换一、arrayList<Map<String, Object>>转化json字符串,存入redis
- 主要功能共有三个角色:管理员、教师、学生。管理员功能有:学生管理、教师管理、评教管理、指标管理、课程管理等。教师功能有:学生管理、指标管理、
- Actuator简介监控分类Actuator 提供Rest接口,展示监控信息。接口分为三大类:应用配置类:获取应用程序中加载的应用配置、环境
- 有项目需求需要绘制多个圆圈,并且使用连续的数字对其排列起来,也就是好多排的圆圈。首先看一下效果图:一排设置为8个,一共有53个的:一排设值为
- 1. SpringBoot ResponseBody 返回值中null值处理@PostMapping(path = "/test&
- 在公司的项目中用到了分布式锁,但只会用却不明白其中的规则所以写一篇文章来记录使用场景:交易服务,使用redis分布式锁,防止重复提交订单,出
- 前言:在android HTML5 开发中有不少人遇到过 audio 标签 autoplay在某些设备上无效的问题,网上大多是讲怎么在js中
- 什么是 terms set 查询?Terms set 查询根据匹配给定字段的精确术语的最少数量返回文档。terms set 查询与 term