Java Word Appache POI-垂直对齐单元格内容/在文本后删除空格

问题描述 投票:0回答:1

我的桌子看起来像这样:Word POI Table

如您所见,文本与单元格的顶部对齐。我想使其垂直居中或与单元格底部对齐。我尝试使用cell.setVerticalAlignment(XWPFVertAlign.CENTER);实现它,但它没有任何改变。我能够通过table.setCellMargin()向单元格添加边距。它在单元格的顶部增加了边距,但是文本下方的空白使单元格太大。也许有一种方法可以使单元格的宽度适合文本的高度?我的预期结果将如下所示:desired result

这是我的代码的一部分:`

XWPFTable table = document.createTable();
CTTblWidth tableIndentation = table.getCTTbl().getTblPr().addNewTblInd();
tableIndentation.setW(BigInteger.valueOf(720));
tableIndentation.setType(STTblWidth.DXA);

XWPFTableRow tableRowOne = table.getRow(0);
XWPFParagraph paragraphTable = tableRowOne.getCell(0).addParagraph();
table.setCellMargins(0, 50, 0, 0);
XWPFRun runT = paragraphTable.createRun();

runT.setBold(true);
runT.setColor("ffffff");
runT.setText("REFERENCE ACTIVITIES PROVIDED: " + String.valueOf(def.format(c.getEarnedSum())) + " POINTS EARNED");
runT = tableRowOne.addNewTableCell().addParagraph().createRun();
runT = tableRowOne.addNewTableCell().addParagraph().createRun();

tableRowOne.getCell(0).getCTTc().addNewTcPr().addNewShd().setFill("8dc63f");
tableRowOne.getCell(1).getCTTc().addNewTcPr().addNewShd().setFill("8dc63f");
tableRowOne.getCell(2).getCTTc().addNewTcPr().addNewShd().setFill("8dc63f");
tableRowOne.getCell(0).removeParagraph(0);
tableRowOne.getCell(1).removeParagraph(0);
tableRowOne.getCell(2).removeParagraph(0);`
java apache-poi word
1个回答
0
投票

XWPFTableCell.setVerticalAlignment有效。但是必须知道,每个段落之后的默认间距均为10pt。因此,段落高度是文字高度加10pt。然后那个高度是垂直对齐的。

因此,为了使XWPFTableCell.setVerticalAlignment正常工作,您需要将段落后的间距设置为0。

完整示例:

import java.io.FileOutputStream;
import org.apache.poi.xwpf.usermodel.*;

public class CreateWordTableVerticalAlign {

 public static void main(String[] args) throws Exception {

  XWPFDocument document= new XWPFDocument();

  XWPFParagraph paragraph = document.createParagraph();
  XWPFRun run=paragraph.createRun();  
  run.setText("The table:");

  //create the table
  XWPFTable table = document.createTable(3,3);
  table.setWidth("100%");
  for (int r = 0; r < 3; r++) {
   XWPFTableRow row = table.getRow(r);

   row.setHeight(1024/2); // 1/2inch; 1024Twip == 1024/20 == 72pt == 1inch 

   for (int c = 0; c < 3; c++) {
    XWPFTableCell cell = row.getCell(c);
    cell.setText("row " + r + ", col " + c);

    // get first paragraph in cell - this contains the content set above by cell.setText
    XWPFParagraph firstParagraphInCell = cell.getParagraphArray(0);
    // set spacing after to 0 (defaults to 10pt)
    firstParagraphInCell.setSpacingAfter(0);

    if (r == 0) {
     // default vertical align
    } else if (r == 1) {
     cell.setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
    } else if (r == 2) {
     cell.setVerticalAlignment(XWPFTableCell.XWPFVertAlign.BOTTOM);
    }
   }
  }

  FileOutputStream out = new FileOutputStream("CreateWordTableVerticalAlign.docx"); 
  document.write(out);
  out.close();
  document.close();
 }
}

结果:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.