java - 如何在 java 中使用 apache poi 降低 word 中表格行的默认高度

标签 java ms-word apache-poi

我正在使用 Apache Poi 创建 word,我无法降低行高。我找到了两种设置高度的方法,但都不起作用。我使用了以下片段。

int nRows2 = 6;
int nCols2 = 3;
XWPFTable table2 = doc.createTable(nRows2, nCols2);

CTTblWidth width2 = table2.getCTTbl().addNewTblPr().addNewTblW();
width2.setType(STTblWidth.DXA);
width2.setW(BigInteger.valueOf(13000));

XWPFTableRow testingrow = table2.getRow(0);

CTTblPr testingTblPr = table2.getCTTbl().getTblPr();
CTString sstyleStr = testingTblPr.addNewTblStyle();
sstyleStr.setVal("StyledTable");


CTTrPr trPr2 = testingrow.getCtRow().addNewTrPr();
CTHeight ht2 = trPr2.addNewTrHeight();
ht2.setVal(BigInteger.valueOf(2));
  System.out.println("height is "+testingrow.getHeight());
//tableRowOne.setHeight(0);
  testingrow.getCell(0).setText("vijay ");
  testingrow.getCell(0).setColor("123456");

//  Second method is just setting height from row object 
testingrow.setHeight(2);

最佳答案

XWPFTableRow.setHeight(int height) https://poi.apache.org/apidocs/org/apache/poi/xwpf/usermodel/XWPFTableRow.html#setHeight%28int%29为我工作。

高度必须设置为 Twips(二十分之一英寸点)。

但是如果你想将行高降低到默认行高以下,这取决于字体大小,那么你必须设置 w:hRule="exact"。这只能使用底层对象并在类路径中包含 ooxml-schemas-1.3.jar,如 https://poi.apache.org/faq.html#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.XWPFTableRow;

import org.openxmlformats.schemas.wordprocessingml.x2006.main.STHeightRule;
/*
To
org.openxmlformats.schemas.wordprocessingml.x2006.main.STHeightRule;
the fully ooxml-schemas-1.3.jar is needed as mentioned in https://poi.apache.org/faq.html#faq-N10025
*/

public class CreateTable 
{
   public static void main(String[] args)throws Exception 
   {
   //Blank Document
   XWPFDocument document= new XWPFDocument();

   //Write the Document in file system
   FileOutputStream out = new FileOutputStream(
   new File("create_table.docx"));

   //create table
   XWPFTable table = document.createTable();
   //create first row
   XWPFTableRow tableRowOne = table.getRow(0);
   tableRowOne.getCell(0).setText("col one, row one");
   tableRowOne.addNewTableCell().setText("col two, row one");
   tableRowOne.addNewTableCell().setText("col three, row one");
   //create second row
   XWPFTableRow tableRowTwo = table.createRow();
   tableRowTwo.getCell(0).setText("col one, row two");
   tableRowTwo.getCell(1).setText("col two, row two");
   tableRowTwo.getCell(2).setText("col three, row two");

int twipsPerInch =  1440;
tableRowTwo.setHeight((int)(twipsPerInch*1/10)); //set height 1/10 inch.
tableRowTwo.getCtRow().getTrPr().getTrHeightArray(0).setHRule(STHeightRule.EXACT); //set w:hRule="exact"

   //create third row
   XWPFTableRow tableRowThree = table.createRow();
   tableRowThree.getCell(0).setText("col one, row three");
   tableRowThree.getCell(1).setText("col two, row three");
   tableRowThree.getCell(2).setText("col three, row three");

twipsPerInch =  1440;
tableRowThree.setHeight(twipsPerInch*1); //set height 1 inch.

   document.write(out);
   out.close();
   System.out.println("create_table.docx written successully");
   }
}

关于java - 如何在 java 中使用 apache poi 降低 word 中表格行的默认高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36545834/

相关文章:

c# - 程序在 oWord.Documents.Open(oTemplatePath) 处卡住?

php - 如何使用 php 脚本计算 .doc 文件中的单词数?

excel - 如何使用 VBA 将 Excel 中的一系列单元格粘贴到 Word 中

java - 在 Box2D (libgdx) 中移除主体会立即导致游戏崩溃

java - ANTLR 4 : Parsing grammar

Android + Proguard + Apache POI

image - 如何使用 apache poi 在 Excel 中集中对齐图像

java - 读取单列中包含多个值的 Excel 文件 - Java

java - 在私有(private) java 变量中使用它

java - 如何比较java中的文档对象与XERCES?