开发者

Controlling table row height for document in poi

开发者 https://www.devze.com 2023-03-20 11:00 出处:网络
I am trying to draw a table in poi using XWPF component using the below code // InputStream in= Temp.class.getResourceAsStream(\"Sample.docx\");

I am trying to draw a table in poi using XWPF component using the below code

     // InputStream in= Temp.class.getResourceAsStream("Sample.docx");

      XWPFDocument doc = new XWPFDocument();

      XWPFTable table=doc.createTable(2,2);

     //  CTDecimalNumber rowSize = table.getCTTbl().getTblPr().getTblStyleRowBandSize();
      // rowSize.setVal(new BigInteger("10"));
     // table.getRow(1).setHeight(0);
      table.getRow(0).getCell(0).setText("Row-0-Cell-0");
      table.getRow(0).getCell(1).setText("Row-0-Cell-1");
      table.getRow(1).getCell(0).setText("Row-1-Cell-0");
      table.getRow(1).getCell(1).setText("Row-1-Cell-1");

      FileOutputStream out = new FileOutputStream("simpleTable.docx");
      doc.write(out);
      out.close();

It draws the table properly but The height of the cells are too big and width also does not falls properly in place. I saw in a note that the table are supposed to auto fit as per the content. Tried couple of things which are as follows:

  • Tried setting the height as shown in the commented code above but that did not worked.
  • Tried reading an existing doc as shown in inputstream commented code. That gave an exception that could not read a poi-ooxml file.
  • Tried using TblStyleRowBandSize but that always remains null. Not sure how to create a new instance of CTDecimalNumber or TblStyleRowBandSize

thanks in advance.

Some more insight: When I create an empty table, and add rows and column by using create, it works fine and does not inserts the formatting character. But making an empty table results in a cell created at the begining and I am still trying to find a way to remove that first column. New code

   XWPFTable tabl开发者_开发知识库e=doc.createTable();

    XWPFTableRow row1 =  table.createRow();
    row1.createCell().setText("Row-0-Cell-0");
    row1.createCell().setText("Row-0-Cell-1");
    XWPFTableRow row2 =  table.createRow();
    row2.createCell().setText("Row-1-Cell-0");
    row2.createCell().setText("Row-1-Cell-1");


I'm not sure on most of your query, but I can help with the last bit, you'll want something like:

    if(! table.getTblPr().isSetTblStyleRowBandSize()) {
       table.getTblPr().addNewTblStyleRowBandSize();
    }
    System.out.println("Was " + table.getTblPr().getTblStyleRowBandSize().getVal());
    table.getTblPr().getTblStyleRowBandSize().setVal(new BigInteger("12345"));
    System.out.println("Now " + table.getTblPr().getTblStyleRowBandSize().getVal());
0

精彩评论

暂无评论...
验证码 换一张
取 消