답 일지 몰라도 답답함은 해결되길

IT

[IT] Create Simple Project for handling docx using Apache POI Part 2/2

아담도라이바 2020. 10. 18. 21:52
반응형

[IT] Create Simple Project for handling docx using Apache POI Part 1/2

In the last post, I checked the Simple Maven Project Creation and "pom.xml".

In this post, Let's add a component necessary for Word document editing to "pom.xml" and then print a test documnet.

@ Apache POI's Artifacts

Artifacts  For Apache POI

This is a list of Artifacts required for editing Word documents.

You should refer to this and add them to <Defendencies> of "pom.xml".

edited pom.xml

The added content is shown in the figure below.

pom.xml with dependencies for Word

 

@ Creation of  Simple Project to read Word document

When the "pom.xml" configuration is finished, create a class to test the function.

Once you are going to print the contents to the console, select "public static void main(Stringp[] args)".

New Simple Project

 

If the class was created normally, enter the test code below.

Edit Code

package myhome.testprj;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
public class WordSimplePrintTest {

public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub

    File wordFile = new File ("/Users/yongsu/_DevWorkSpace/testdocx/simpledoc.docx");
    FileOutputStream out = new FileOutputStream ("/Users/yongsu/_DevWorkSpace/testdocx/simpledoc_Result.docx");
    FileInputStream fis = new FileInputStream(wordFile.getAbsolutePath());

    XWPFDocument document = new XWPFDocument(fis);
    List paragraphList = document.getParagraphs();

    for (XWPFParagraph para:paragraphList) {
        System.out.println(para.getText());
    }

    document.close();
    out.close();

    fis.close();
    }
}

For the path of .docx that loads File from source and the path of using FileOutputStream, enter an appropriate path for you.

Debuging

Excute [Debug As] - [Java Application] of the class and check the result.

If the contents of the document you created are displayed in the console tab, the environment in which you can check the Word functions is configured.

From next time, let's look at the code necessary to actually implement the function.

반응형