简体   繁体   中英

Document created with Apache POI is different in Microsoft Word than in LibreOffice Writer

I'm trying to create docx documents using Apache POI. However, when I open the created documents in Microsoft Word, the result is different from when I open it with LibreOffice Writer. I experienced this behavior with tables and cross references.

I would like to know if it is necessary to make any configuration to save the output format in Microsoft Word format.

For example, the code below creates a docx document containing a table with 2 rows and 2 columns:

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;

public class DocWithTable {

    public static final int NUM_ROWS = 2;
    public static final int NUM_COLS = 2;

    public static void main(String[] args) throws IOException {
        XWPFDocument document = new XWPFDocument();

        XWPFTable table = document.createTable();
        XWPFTableRow firstRow = table.getRow(0);
        for (int c = 1; c < NUM_COLS; c++) {
            firstRow.addNewTableCell().getCTTc().addNewTcPr();
        }
        for (int r = 1; r < NUM_ROWS; r++) {
            table.createRow();
        }

        FileOutputStream out = new FileOutputStream(new File("file.docx"));
        document.write(out);
        out.close();
        document.close();
    }

}

When I open the create document with Microsoft Word, I get the following result:

在Microsoft Word中打开文档

When I open it with LibreOffice Writer, I get the following result:

在LibreOffice Writer中打开文档

Also, after saving the document in LibreOffice, it is opened as expected in Microsoft Word.

Your table cells are all empty. For this the behavior really is different. If no explicit width is set, in Microsoft Word all table cells are as width as the content needs, in LibreOffice Writer the table width is 100% of usable page width.

Using apache poi 4.1.0 you could set the table width 100% using XWPFTable.setWidth to make the behavior equal.

in your code:

...
XWPFTable table = document.createTable();

table.setWidth("100%");
...

For apache poi versions that do not have method XWPFTable.setWidth , the following code can be used:

import java.math.BigInteger;

import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTblWidth;

    ...
    XWPFTable table = document.createTable();
    CTTblWidth ctTblWidth = table.getCTTbl().getTblPr().getTblW();
    ctTblWidth.setType(STTblWidth.PCT);
    ctTblWidth.setW(BigInteger.valueOf(50 * 100)); // 50 times the desired percentage
    ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM