简体   繁体   中英

How to change XWPFTableCell margins in Apache POI in Java?

I tried searching too much for this but did not get the desired result. I am using Apache POI to copy specific ata from an Excel sheet to a table in MS Word 2010 file using XWPF. I have completed that.

The last thing I want to do is to add a small left and right margin to each cell so that text does not stick to the cell borders. I searched for everything on internet but was unable to do so. Maybe I am missing something.

You can set a cell margin at the table level:

table.setCellMargins(0, 500, 0, 500);

Complete example would look like this:

public static void main(String[] args) throws IOException {
    XWPFDocument doc = new XWPFDocument();
    FileOutputStream out = new FileOutputStream(new File(FILENAME));

    XWPFParagraph para = doc.createParagraph();
    XWPFRun run = para.createRun();

    //table
    XWPFTable table = doc.createTable();
    table.setCellMargins(0, 500, 0, 500); //set margins here

    //rows
    XWPFTableRow row1 = table.getRow(0);
    row1.getCell(0).setText("Hello1");
    row1.addNewTableCell().setText("Hello2");
    row1.addNewTableCell().setText("Hello3");

    XWPFTableRow row2 = table.createRow();
    row2.getCell(0).setText("Hello4");
    row2.getCell(1).setText("Hello5");
    row2.getCell(2).setText("Hello6");

    doc.write(out);
    out.close();
    doc.close();
}

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