我尝试了太多的搜索,但没有得到想要的结果。我正在使用Apache POI使用XWPF将特定的ata从Excel工作表复制到MS Word 2010文件中的表格。我已经完成了这项工作。
我想做的最后一件事是为每个单元格添加一个小的左右边距,这样文本就不会粘在单元格的边界上。我在网上搜索了所有的东西,但是没有找到。也许我错过了什么。
发布于 2017-09-28 21:56:41
您可以在表级设置单元格边距:
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();
}
https://stackoverflow.com/questions/46468753
复制相似问题