使用 Java POI 插入表格时 Open office writer 崩溃 [英] Open office writer crashed when inserting table using java POI

查看:24
本文介绍了使用 Java POI 插入表格时 Open office writer 崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to insert table using apache-poi in .docx format using open office..But the file is crashed every time i open the file

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

推荐答案

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

Libreoffice/Openoffice needs a table grid to accept the column widths.

这应该有效:

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

顺便说一句:请务必在表格前后至少放置一段.否则真的不可能把光标移到表外.

Btw.: Please always have at least one paragraph before and after the table. Else it is really impossible to get the cursor outside the table.

这篇关于使用 Java POI 插入表格时 Open office writer 崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆