Apache POI Word如何消除表格、文本、段落等所有间距?
解决Apache POI生成Word时的内容间距问题
问题说明
使用Apache POI生成Word文档时,表格、段落及文本之间存在多余间距,需实现类似Word样式中「无间距」的效果,原测试代码如下:
public class PoiTest { public static void main(String[] args) throws IOException { XWPFDocument doc = new XWPFDocument(); XWPFTable table = doc.createTable(); XWPFTableRow row1 = table.getRow(0); row1.getCell(0).setText("First Row, First Column"); row1.addNewTableCell().setText("First Row, Second Column"); row1 = table.createRow(); row1.getCell(0).setText("Second Row, First Column"); row1.getCell(1).setText("Second Row, Second Column"); row1 = table.createRow(); row1.getCell(0).setText("Third Row, First Column"); row1.getCell(1).setText("Third Row, Second Column"); XWPFParagraph title = doc.createParagraph(); XWPFRun titleRun = title.createRun(); titleRun.setText("First"); title = doc.createParagraph(); titleRun = title.createRun(); titleRun.setText("Second"); FileOutputStream out; try { out = new FileOutputStream("c:\\test\\test.docx"); doc.write(out); System.out.println("ok!!!"); if (out != null) { out.close(); } } catch (Exception e) { e.printStackTrace(); } } }
解决方案
要消除所有内容间距,需从表格和段落的样式设置入手:
1. 调整表格间距
- 设置表格单元格内边距为0,消除单元格内部空白
- 统一单元格内段落的间距,避免单元格文本出现多余空白
- 调整表格所在段落的前后间距,消除表格与上下内容的空隙
2. 调整段落间距
- 新建段落时设置前后间距为0
- 固定行间距,避免段落内或段落间出现多余行距
修改后的完整代码
import org.apache.poi.xwpf.usermodel.*; import java.io.FileOutputStream; import java.io.IOException; import java.math.BigInteger; public class PoiTest { public static void main(String[] args) throws IOException { XWPFDocument doc = new XWPFDocument(); // 创建表格并设置基础样式 XWPFTable table = doc.createTable(); // 设置表格单元格内边距为0 table.setCellMargins(0, 0, 0, 0); // 固定表格宽度,取消自动调整 table.getCTTbl().addNewTblPr().addNewTblW().setType(STTblWidth.DXA); table.getCTTbl().getTblPr().getTblW().setW(BigInteger.valueOf(9072)); // 适配常规页面宽度,可按需调整 // 处理第一行单元格 XWPFTableRow row1 = table.getRow(0); setCellParagraphStyle(row1.getCell(0)); row1.getCell(0).setText("First Row, First Column"); XWPFTableCell cell2 = row1.addNewTableCell(); setCellParagraphStyle(cell2); cell2.setText("First Row, Second Column"); // 处理第二行单元格 row1 = table.createRow(); setCellParagraphStyle(row1.getCell(0)); row1.getCell(0).setText("Second Row, First Column"); setCellParagraphStyle(row1.getCell(1)); row1.getCell(1).setText("Second Row, Second Column"); // 处理第三行单元格 row1 = table.createRow(); setCellParagraphStyle(row1.getCell(0)); row1.getCell(0).setText("Third Row, First Column"); setCellParagraphStyle(row1.getCell(1)); row1.getCell(1).setText("Third Row, Second Column"); // 设置表格所在段落的无间距样式 XWPFParagraph tablePara = doc.getParagraphArray(doc.getParagraphs().size() - 1); setNoSpacingParagraphStyle(tablePara); // 创建无间距文本段落 XWPFParagraph title = doc.createParagraph(); setNoSpacingParagraphStyle(title); XWPFRun titleRun = title.createRun(); titleRun.setText("First"); title = doc.createParagraph(); setNoSpacingParagraphStyle(title); titleRun = title.createRun(); titleRun.setText("Second"); // 输出文档 FileOutputStream out; try { out = new FileOutputStream("c:\\test\\test.docx"); doc.write(out); System.out.println("ok!!!"); if (out != null) { out.close(); } } catch (Exception e) { e.printStackTrace(); } } // 统一设置单元格内段落为无间距样式 private static void setCellParagraphStyle(XWPFTableCell cell) { for (XWPFParagraph para : cell.getParagraphs()) { setNoSpacingParagraphStyle(para); } } // 设置段落为无间距样式 private static void setNoSpacingParagraphStyle(XWPFParagraph para) { // 段落前后间距设为0 para.setSpacingBefore(0); para.setSpacingAfter(0); // 固定行间距(240 twips = 12磅,可根据字体大小调整) para.setLineSpacingRule(LineSpacingRule.EXACT); para.setLineSpacing(240); // 取消首行缩进 para.setIndentationFirstLine(0); } }
关键说明
setNoSpacingParagraphStyle方法统一处理段落的间距、行缩进和行间距,确保无多余空白setCellParagraphStyle遍历单元格内所有段落,应用无间距样式,解决单元格内部的间距问题- 表格额外设置了固定宽度和0内边距,避免表格自身产生空白
内容的提问来源于stack exchange,提问作者keep
相关产品推荐
相关产品推荐

