Get the App
SLTechnology News&Howtos  ›  Development  › 

How to read the contents of txt file and generate Word document by Java

Shulou Source: shulou.com Published: 2022-06-02 08:10:13 10月04日 Update

This article will explain in detail how Java can read the contents of txt files and generate Word documents. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

Import Jar package

There are two ways to import jar files in Java programs.

1. Download and import Maven warehouse

The configuration in pom.xml is as follows:

Com.e-iceblue https://repo.e-iceblue.cn/repository/maven-public/ e-iceblue spire.doc.free 3.9.0 2. Manual import

You need to download the jar package locally, decompress it, and find the jar file under the lib path. Then open the Project Structure window in the Java program, and then perform the following steps to import:

Find the jar file under the local path, add it to the list, and import:

Read txt to generate Word

The code is roughly as follows:

Instantiate an object of the Document class. Then add sections and paragraphs through the Document.addSection () method and the Section.addParagraph () method.

Read the txt file: create an object of the InputStreamReader class, passing the input stream and the specified coding table name in the constructor. Create a character stream buffer through the BufferedReader class. The read txt content is added to the paragraph through the Paragraph.appendText () method.

Call the Document.saveToFile (string fileName, FileFormat fileFormat) method to save as an Word document.

Import com.spire.doc.*;import com.spire.doc.documents.Paragraph;import com.spire.doc.documents.ParagraphStyle;import java.awt.*;import java.io.*;public class ReadTextAndCreateWord {public static void main (String [] args) throws IOException {/ / instantiate the object of the Document class and add section and paragraph Document doc = new Document (); Section section = doc.addSection (); Paragraph paragraph = section.addParagraph () / / read txt file String encoding = "GBK"; File file = new File ("test.txt"); if (file.isFile () & & file.exists ()) {InputStreamReader isr = new InputStreamReader (new FileInputStream (file), encoding); BufferedReader bufferedReader = new BufferedReader (isr); String lineTXT While ((lineTXT = bufferedReader.readLine ())! = null) {paragraph.appendText (lineTXT); / / write txt content in a paragraph} isr.close ();} / / style the paragraph and apply it to the paragraph ParagraphStyle style = new ParagraphStyle (doc); style.setName ("newstyle") Style.getCharacterFormat (). SetBold (true); style.getCharacterFormat (). SetTextColor (Color.BLUE); style.getCharacterFormat (). SetFontName ("young circle"); style.getCharacterFormat (). SetFontSize (12); doc.getStyles (). Add (style); paragraph.applyStyle ("newstyle"); paragraph.getFormat (). SetMirrorIndents (true) / / Save to Word doc.saveToFile ("addTxttoWord.docx", FileFormat.Docx_2013) in docx format; doc.dispose ();}}

Word creation result:

Matters needing attention

The txt file and word save path in the code is an IDEA program project folder, such as: F:\ IDEAProject\ CreateWord_Doc\ addTxttoWord.docx, and the file path can be defined as another path.

About Java how to read the contents of txt files and generate Word documents to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Tags: Files contents methods paragraphs paths documents builds objects programs code examples articles more steps knowledge articles good items repositories available in Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Microsoft MySQL Shulou Tech Info Huawei