XWPFTable 中的文本方向

1 java apache apache-poi xwpf

如何在 XWPFTable 中使用 Apache POI 将文本旋转 90 度?

所以它看起来像这样

Axe*_*ter 5

文本方向设置XWPFTableCell直到现在才实现。但是使用getCTTc我们可以获得底层CTTc对象。从这里我们可以设置addNewTcPr()addNewTextDirection()

使用org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTextDirection这个例子需要FAQ-N10025中ooxml-schemas-1.3.jar提到的所有模式的完整jar 。

例子:

import java.io.File;
import java.io.FileOutputStream;

import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;

import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTextDirection;

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

  XWPFDocument document = new XWPFDocument();

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

  XWPFTable table = document.createTable(1,3);
  for (int r = 0; r < 1; r++) {
   for (int c = 0 ; c < 3; c++) {
    XWPFTableCell tableCell = table.getRow(r).getCell(c);
    tableCell.getCTTc().addNewTcPr().addNewTextDirection().setVal(STTextDirection.BT_LR);
    paragraph = tableCell.getParagraphArray(0);
    run = paragraph.createRun();  
    run.setText("text");
   }
  }

  paragraph = document.createParagraph();

  document.write(new FileOutputStream("CreateWordTableTextVertical.docx"));
  document.close();

 }
}
Run Code Online (Sandbox Code Playgroud)