本文介绍了XWPFTable 中的文本方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How to rotate text using Apache POI in XWPFTable to 90 degrees?

所以它看起来像这样

推荐答案

直到现在,文本方向设置还没有在 XWPFTableCell 中实现.但是使用 getCTTc 我们可以获取底层 CTTc 对象.从这里我们可以设置 addNewTcPr(), addNewTextDirection().

The text direction settings are not implemented into XWPFTableCell until now. But using getCTTc we can get the underlaying CTTc object. And from this we can set addNewTcPr(), addNewTextDirection().

为了使用 org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTextDirection 这个例子需要所有模式的完整 jar ooxml-schemas-1.3.jarFAQ-N10025中所述.

For using org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTextDirection this example needs the full jar of all of the schemas ooxml-schemas-1.3.jar as mentioned in the FAQ-N10025.

示例:

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

 }
}

这篇关于XWPFTable 中的文本方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 12:17