vij*_*mar 5 java ms-word apache-poi
我正在使用Apache Poi来创建单词,我无法降低行高.我找到了两种设置高度的方法,但两种方法都不起作用.我使用了以下代码段.
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);
Run Code Online (Sandbox Code Playgroud)
该XWPFTableRow.setHeight(int height) https://poi.apache.org/apidocs/org/apache/poi/xwpf/usermodel/XWPFTableRow.html#setHeight%28int%29对我的作品.
高度必须设置为Twips(英寸点的二十分之一).
但是,如果要将行高度降低到默认行高以下(这取决于字体大小),则必须设置w:hRule="exact".这只能使用底层对象并具有https://poi.apache.org/faq.html#faq-N10025中ooxml-schemas-1.3.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.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");
}
}
Run Code Online (Sandbox Code Playgroud)