使用java POI插入表时,打开的办公室编写器崩溃了

时间:2017-06-23 06:40:06

标签: java apache-poi

我正在尝试使用开放式办公室以.docx格式使用apache-poi插入表。但是每次打开文件时文件都会崩溃

      XWPFDocument document= new XWPFDocument();
      FileOutputStream out = new FileOutputStream(new File("C://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");

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

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

1 个答案:

答案 0 :(得分:0)

Libreoffice / Openoffice需要一个表格来接受列宽。

这应该有效:

import java.io.FileOutputStream;

import org.apache.poi.xwpf.usermodel.*;

import java.math.BigInteger;

public class CreateWordTable {
 public static void main(String[] args) throws Exception {

  XWPFDocument document = new XWPFDocument();

  XWPFParagraph paragraph = document.createParagraph();

      //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 CTTblGrid for this table with widths of the 3 columns. 
  //necessary for Libreoffice/Openoffice to accept the column widths.
  //values are in unit twentieths of a point (1/1440 of an inch)
  //first column = 2 inches width
  table.getCTTbl().addNewTblGrid().addNewGridCol().setW(BigInteger.valueOf(2*1440));
  //other columns (2 in this case) also each 2 inches width
  for (int col = 1 ; col < 3; col++) {
   table.getCTTbl().getTblGrid().addNewGridCol().setW(BigInteger.valueOf(2*1440));
  }

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

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

  paragraph = document.createParagraph();

  document.write(new FileOutputStream("CreateWordTable.docx"));
  document.close();

  System.out.println("CreateWordTable written successully");
 }
}

顺便说一句:请在表格前后至少有一个段落。否则,将光标移到桌子外是不可能的。