Как уменьшить высоту строки таблицы по умолчанию в word с помощью Apache poi в java



Я использую 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);
755   2  

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");
   }
}

Чтобы дополнить ответ Акселя Рихтера, если вы хотите избавиться от белого пространства абзаца в ячейках, чтобы текст не заканчивался в ячейке, просто удалите абзац в ячейке и создайте свой собственный, как это:

XWPFTable table= document.createTable();
XWPFTableRow tableRow = table.getRow(0);
tableRow.addNewTableCell();
tableRow.getCell(0).removeParagraph(0);
XWPFParagraph paragraph = tableRow.getCell(0).addParagraph();

Comments

    Ничего не найдено.