当前位置:网站首页>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 :

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—

原网站

版权声明
本文为[Eiceblue]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206111133421479.html