2

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 table=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");
Danilo Piazzalunga
  • 7,590
  • 5
  • 49
  • 75
lalit
  • 1,485
  • 2
  • 17
  • 32
  • I think the autofit is working but what is happening is that there are extra line breaks inserted which I see after turning the visibility of formatting symbols. Not sure why XWPF is inserting those formatting in the cell. – lalit Jul 15 '11 at 05:49

1 Answers1

0

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());
Gagravarr
  • 47,320
  • 10
  • 111
  • 156
  • Thanks for reply. getTbalPr is private so changed it to table.getCTTbl().getTblPr(). However I realized the issue is more of insertion of formatting symbol as I have put in the comment of the question. – lalit Jul 15 '11 at 05:50