How to change XWPFTableCell margins in Apache POI in Java?
Asked Answered
I

1

5

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.

Intercessory answered 28/9, 2017 at 12:7 Comment(0)
I
8

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();
}
Imitation answered 28/9, 2017 at 13:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.