如何在 XWPFTable 中使用 Apache POI 将文本旋转 90 度?
问问题
750 次
2 回答
5
XWPFTableCell
直到现在还没有实现文本方向设置。但是使用getCTTc我们可以获得底层的CTTc对象。从这里我们可以设置addNewTcPr(),addNewTextDirection()。
使用org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTextDirection
此示例需要FAQ-N10025ooxml-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();
}
}
于 2017-03-25T07:22:23.307 回答
0
您可以使用此方法从 apache poi liberary 中设置各种 ENUM alighments。STJc.Enum 支持各种对齐方式。
public void setTableAlignment(XWPFTable table, STJc.Enum justification) {
CTTblPr tblPr = table.getCTTbl().getTblPr();
CTJc jc = (tblPr.isSetJc() ? tblPr.getJc() : tblPr.addNewJc());
jc.setVal(justification);
}
于 2017-06-20T09:55:01.737 回答