In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
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.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.