Apache POI Word(docx) getting started example

Apache POI Word(docx) getting started sample tutorial

1. Document XWPFDocument

XWPFDocument is right docx is a high-level encapsulation API for document operation.

1.1. create a new document

XWPFDocument doc = new XWPFDocument();

1.2. Read existing documents: paragraphs, tables, pictures

XWPFDocument doc = new XWPFDocument(new FileInputStream("./deepoove.docx"));

Next, we can read the contents of the document, including paragraphs, tables, pictures, etc

// paragraph
List<XWPFParagraph> paragraphs = doc.getParagraphs();
// form
List<XWPFTable> tables = doc.getTables();
// picture
List<XWPFPictureData> allPictures = doc.getAllPictures();
// header
List<XWPFHeader> headerList = doc.getHeaderList();
// footer
List<XWPFFooter> footerList = doc.getFooterList();

1.3. Generate document

Write the document to the stream by passing in an OutStream:

try (FileOutputStream out = new FileOutputStream("simple.docx")) {

2. Paragraph XWPFParagraph

Paragraph is a basic unit of Word document.

2.1. Create a new paragraph

XWPFParagraph p1 = doc.createParagraph();

2.2. set paragraph format

// Alignment
// frame

2.3. Basic element XWPFRun

After creating a paragraph, we can process the text and pictures in the paragraph through the relevant API. XWPFRun is the basic unit of a paragraph. It can be a text or a picture.

2.4. Paragraph Text

2.4.1. Read paragraph content

// Get text
String text = paragraph.getText();

// Get all xwpfruns in the paragraph
List<XWPFRun> runs = paragraph.getRuns();

2.4.2. Create XWPFRun text

// Create XWPFRun at the end of the paragraph
XWPFRun run = paragraph.createRun();
run.setText("Append text to this paragraph");

2.4.3. Insert XWPFRun text

// Insert XWPFRun at the beginning of the paragraph
XWPFRun insertNewRun = paragraph.insertNewRun(0);
insertNewRun.setText("Insert this text at the beginning of the paragraph");

2.4.4. Modify XWPFRun text

List<XWPFRun> runs = paragraph.getRuns();
// setText defaults to additional text, and parameter 0 indicates setting the text at position 0, overwriting the previous setting
runs.get(0).setText("Append text", 0);
runs.get(0).setText("Modify text", 0);

2.4.5. Style: color, font

// colour
// Italics
// bold
// typeface
// Underline

2.4.6. Wrap text


2.5. Paragraph picture

2.5.1. Extract picture XWPFPicture

List<XWPFPictureData> allPictures = doc.getAllPictures();
XWPFPicture pciture = allPictures.get(0);
byte[] data = pciture.getPictureData().getData();
// Next, you can write the image byte array to the output stream

2.5.2. Create XWPFRun picture

import org.apache.poi.util.Units;

InputStream stream = new FileInputStream("./sayi.png");
XWPFRun run = paragraph.createRun();
run.addPicture(stream, XWPFDocument.PICTURE_TYPE_PNG, "Generated", Units.toEMU(256), Units.toEMU(256));

3. Form XWPFTable

Table is another important basic element of Word document.

3.1. Create a new table

Create a table with three rows and three columns:

XWPFTable table = doc.createTable(3, 3);

3.2. Set cell text

The table is composed of table rows XWPFRow, each row is composed of cells XWPFCell, and the interior of each cell is composed of many XWPFParagraph paragraphs.

table.getRow(1).getCell(1).setText("EXAMPLE OF TABLE");

The above code is equivalent to the following code:

XWPFParagraph p1 = table.getRow(0).getCell(0).addParagraph();
XWPFRun r1 = p1.createRun();
r1.setText("EXAMPLE OF TABLE");

3.3. Set cell picture

In fact, image operation is to obtain the paragraph and then operate the image in the paragraph.

XWPFParagraph p1 = table.getRow(0).getCell(0).addParagraph();
XWPFRun r1 = p1.createRun();
// Same paragraph picture

3.4. Set cell style: background color and alignment

// Background color

// Set alignment after getting cell paragraphs
XWPFParagraph addParagraph = cell.addParagraph();

Word template: tl-4

POI TL (poi template language) is a Word template engine based on Apache POI. For complete documentation, see POI TL official document This section mainly introduces how to use the template poi to enhance the API.

4.1. TableTools

TableTools provides several ways to manipulate tables.

4.1.1. Merge several columns in the same row

// Merge the cells from column 0 to column 8 in the first row
TableTools.mergeCellsHorizonal(table, 1, 0, 8);

4.1.2. Merge several rows in the same column

// Merge the cells from the first row to the ninth row of column 0
TableTools.mergeCellsVertically(table, 0, 1, 9);

4.1.3. Table width

// Set the table width to the maximum width of A4 paper
TableTools.widthTable(table, MiniTableRenderData.WIDTH_A4_FULL, 10);

4.1.4. Table style

// Center table
TableStyle style = new TableStyle();
TableTools.styleTable(table, style);

4.2. NiceXWPFDocument

NiceXWPFDocument , is an enhancement to native , XWPFDocument ,.

4.2.1. Word merge

NiceXWPFDocument main = new NiceXWPFDocument(new FileInputStream("main.docx"));

NiceXWPFDocument sub = new NiceXWPFDocument(new FileInputStream("sub.docx"));

// Merge two documents
NiceXWPFDocument newDoc = main.merge(sub);

// Generate new document
FileOutputStream out = new FileOutputStream("new_doc.docx");

4.3. XWPFParagraphWrapper

XWPFParagraphWrapper is an enhancement to the native XWPFParagraphWrapper.

4.3.1. Insert hyperlink

XWPFParagraphWrapper wrapper = new XWPFParagraphWrapper(paragraph);
XWPFRun hyperRun = wrapper.insertNewHyperLinkRun(0, "http:deepoove.com");

4.3.2. Inserting bookmarks

XWPFParagraphWrapper wrapper = new XWPFParagraphWrapper(paragraph);

Tutorial transferred from http://deepoove.com/poi-tl/apache-poi-guide.html#_%E6%96%87%E6%A1%A3xw , for learning reference only. If there is infringement, please contact to delete.

Tags: Java

Posted by EagerWolf on Sun, 22 May 2022 04:50:03 +0300