当前位置:网站首页>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—
边栏推荐
- MYCAT sub database and sub table
- 17.5 concept, usage, deadlock demonstration and solution of mutex
- 202年最新热门收益较高的年金险产品是什么?
- [file upload vulnerability 05] server suffix detection and bypass experiment (based on upload-labs-3 shooting range)
- Iframe value transfer
- JS 加法乘法错误解决 number-precision
- Golang uses XOR ^ to exchange two variables and encrypt / decrypt them
- 【Go】Gin源码解读
- The no category parents plug-in helps you remove the category prefix from the category link
- Publish WordPress database cache plug-in: DB cache reloaded 3.1
猜你喜欢

Elk - hearthbeat implements service monitoring

Node连接MySql数据库写模糊查询接口

2022 | framework for Android interview -- Analysis of the core principles of binder, handler, WMS and AMS!

Guangdong municipal safety construction data management software 2022 new forms are coming

How does Sister Feng change to ice?
![my. Binlog startup failure caused by the difference between [mysql] and [mysqld] in CNF](/img/bd/a28e74654c7821b3a9cd9260d2e399.png)
my. Binlog startup failure caused by the difference between [mysql] and [mysqld] in CNF

JS addition and multiplication error resolution number precision

The complete manual of the strongest Flink operator is a good choice for the interview~

MSF CS OpenSSL traffic encryption

National multi-year solar radiation spatial distribution data 1981-2022, temperature distribution data, evapotranspiration data, evaporation data, rainfall distribution data, sunshine data, wind speed
随机推荐
National multi-year solar radiation spatial distribution data 1981-2022, temperature distribution data, evapotranspiration data, evaporation data, rainfall distribution data, sunshine data, wind speed
Where is it safer to open an account for soda ash futures? How much does it cost to buy soda ash futures?
Eulato
Use compiler option '--downleveliteration' to allow iteration of iterations
2020-07 study notes sorting
Etcd的运行时重配置
2019年书单
C# 读取txt文件生成Word文档
Modify WordPress management account name plug-in: admin rename extended
Use compiler option ‘--downlevelIteration‘ to allow iterating of iterators 报错解决
Collection of practical WordPress plug-ins (under update)
2022 | framework for Android interview -- Analysis of the core principles of binder, handler, WMS and AMS!
Guangdong municipal safety construction data management software 2022 new forms are coming
WordPress登录页面定制插件推荐
吊打面试官,涨姿势
2019 book list
软件项目管理 7.1.项目进度基本概念
[fragmentary thoughts] thoughts on wavelength, wave velocity and period
WordPress重新生成特色图像插件:Regenerate Thumbnails
How does Sister Feng change to ice?