2017-09-28 61 views
1

我試圖尋找太多,但沒有得到所需的結果。我正在使用Apache POI將特定的ata從excel工作表複製到使用XWPF的ms word 2010文件中的表中。我已經完成了。我想要做的最後一件事是爲每個單元格添加一個小的左右邊距,以便文本不會粘到單元格邊框上。我在網上搜索了一切,但無法做到。可能是我錯過了一些東西。請幫忙。如何更改Java中的Apache POI中的XWPFTableCell頁邊距?

太謝謝你了:)

回答

2

您可以在表級別設置單元格邊距:

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

完整的例子是這樣的:

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(); 
}