当前位置:网站首页>C reads TXT file to generate word document
C reads TXT file to generate word document
2022-06-11 11:52:00 【Eiceblue】
This article will be C# Taking the program code as an example, this paper introduces how to read txt Contents of the file , Generate Word file . Before editing code , You can refer to the following code environment for configuration :
- Visual Studio 2017
- .Net Framework 4.6.1
- Free Spire.Doc for .NET
- .txt file
dll Files installed (3 Methods )
1. adopt NuGet install dll(2 Methods )
1.1 Can be in Visual Studio Open in “ Solution explorer ”, Right click “ quote ”,“ management NuGet package ”, And then the search “Free Spire.Doc”, Click on “ install ”. Wait for the program installation to complete .
1.2 Copy the following to PM Console installation .
Install-Package FreeSpire.Doc -Version 9.9.7
2. Manually add dll quote
Can be manually Download package , Then decompress , find BIN Under folder Spire.Doc.dll. And then in Visual Studio Open in “ Solution explorer ”, Right click “ quote ”,“ Add reference ”, The local path BIN Under folder dll Add a reference to the program .
Read txt Generate Word
- adopt StreamReader(Stream stream, Encoding encoding) The constructor reads the data in the specified path txt file .
- adopt Free Spire.Doc Provided Paragraph.AppendText(string text) Method will read txt Content added to Word The paragraph .
- Last , adopt Document.SaveToFile(string fileName, FileFormat fileFormat) Method as Word, And specify the save path .
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)
{
// Instantiation Document Class object , And add section and paragraph
Document doc = new Document();
Section section = doc.AddSection();
Paragraph paragraph = section.AddParagraph();
// Read txt file
StreamReader sr = new StreamReader("test.txt", Encoding.Default);
string line;
while ((line = sr.ReadLine()) != null)
{
paragraph.AppendText(line);// Write... In the paragraph txt
// Set paragraph style , And apply to paragraphs
ParagraphStyle style1 = new ParagraphStyle(doc);
style1.Name = "titleStyle";
style1.CharacterFormat.Bold = true;
style1.CharacterFormat.TextColor = Color.Purple;
style1.CharacterFormat.FontName = " Song style ";
style1.CharacterFormat.FontSize = 12;
doc.Styles.Add(style1);
paragraph.ApplyStyle("titleStyle");
}
// Save as docx Format 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())
' Instantiation Document Class object , And add section and paragraph
Dim doc As New Document()
Dim section As Section = doc.AddSection()
Dim paragraph As Paragraph = section.AddParagraph()
' Read txt file
Dim sr As New StreamReader("test.txt", Encoding.[Default])
Dim line As String
While (InlineAssignHelper(line, sr.ReadLine())) IsNot Nothing
paragraph.AppendText(line)
' Write... In the paragraph txt
' Set paragraph style , And apply to paragraphs
Dim style1 As New ParagraphStyle(doc)
style1.Name = "titleStyle"
style1.CharacterFormat.Bold = True
style1.CharacterFormat.TextColor = Color.Purple
style1.CharacterFormat.FontName = " Song style "
style1.CharacterFormat.FontSize = 12
doc.Styles.Add(style1)
paragraph.ApplyStyle("titleStyle")
End While
' Save as docx Format 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

matters needing attention
In code txt Files and generated Word The document path is F:\VS2017Project\CreateWordDocument_Doc\CreateWordDocument_Doc\bin\Debug Next , The file path can also be customized .
—End—
边栏推荐
- Template engine - thymeleaf
- Bark – 自己给自己的 iPhone 发推送提醒 – 最简单的推送提醒服务,开源免费
- [file upload vulnerability 06] server file content detection and bypass experiment + image horse production method (based on upload-labs-14 shooting range)
- [file upload vulnerability 05] server suffix detection and bypass experiment (based on upload-labs-3 shooting range)
- JS to realize the rotation chart (riding light). Pictures can be switched left and right. Moving the mouse will stop the rotation
- Count the top k strings with the most occurrences
- Node connects to MySQL database and writes fuzzy query interface
- Use compiler option '--downleveliteration' to allow iteration of iterations
- Let WordPress support registered users to upload custom avatars
- 刷题笔记(十四)--二叉树:层序遍历和DFS,BFS
猜你喜欢

JS interview questions - arrow function, find and filter some and every

The tutor transferred me 800 yuan and asked me to simulate a circuit (power supply design)

浙大联合微软亚研院发布视频识别新方法,可对视频逐帧识别且无需,数据标记,或可用于手语翻译等

Apple mobileone: the mobile terminal only needs 1ms of high-performance backbone

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

MYCAT sub database and sub table

C# 设置或验证 PDF中的文本域格式

MSF CS OpenSSL traffic encryption

ELK - ElastAlert最大的坑

js面试题---箭头函数,find和filter some和every
随机推荐
How does Sister Feng change to ice?
Interview experience of Xiaomi Android development post~
Pan domain SSL certificate, sectigo cheap wildcard certificate popularization plan
[file upload vulnerability 05] server suffix detection and bypass experiment (based on upload-labs-3 shooting range)
2022 | framework for Android interview -- Analysis of the core principles of binder, handler, WMS and AMS!
my. Binlog startup failure caused by the difference between [mysql] and [mysqld] in CNF
Adapter mode -- can you talk well?
JS merge two objects (interview questions)
JVM-类加载过程
17.4 creating multiple threads, data sharing problem analysis and case code
Where is it safer to open an account for soda ash futures? How is the deposit calculated?
Gerber文件在PCB制造中的作用
202年最新热门收益较高的年金险产品是什么?
Etcd介绍
Create a folder in the WordPress Library
Centos7.x下安装mysql8遇到的问题Couldn‘t open file /etc/pki/rpm-gpg/RPM-GPG-KEY-mysql-2022
typescript 编译选项和配置文件
【碎碎念】关于波长|波速|周期的想法
JS addition and multiplication error resolution number precision
iframe 传值