使用java POI插入表时,Open office writer崩溃
我试图使用OpenOffice以.docx格式使用ApachePOI插入表。但是每次打开该文件时,该文件都会崩溃使用java POI插入表时,Open office writer崩溃,java,apache-poi,Java,Apache Poi,我试图使用OpenOffice以.docx格式使用ApachePOI插入表。但是每次打开该文件时,该文件都会崩溃 XWPFDocument document= new XWPFDocument(); FileOutputStream out = new FileOutputStream(new File("C://create_table.docx")); //create table XWPFTable table = document.cre
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需要一个表格网格来接受列宽 这应该起作用:
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");
}
}
import java.io.FileOutputStream;
导入org.apache.poi.xwpf.usermodel.*;
导入java.math.biginger;
公共类CreateWordTable{
公共静态void main(字符串[]args)引发异常{
XWPFDocument document=新的XWPFDocument();
XWPFParagraph paragraph paragraph=document.createParagraph();
//创建表
XWPFTable table=document.createTable();
//创建第一行
XWPFTableRow tableRowOne=table.getRow(0);
tableRowOne.getCell(0.setText(“第一列,第一行”);
tableRowOne.addNewTableCell().setText(“第二列,第一行”);
tableRowOne.addNewTableCell().setText(“第三列,第一行”);
//为此表创建宽度为3列的CTTblGrid。
//Libreoffice/Openoffice需要接受列宽。
//数值单位为点的二十分之一(1/1440英寸)
//第一列=2英寸宽
table.getCTTbl().addnewtbgrid().addNewGridCol().setW(biginger.valueOf(2*1440));
//其他列(本例中为2列)的宽度也各为2英寸
for(int col=1;col<3;col++){
table.getCTTbl().getTblGrid().addNewGridCol().setW(biginger.valueOf(2*1440));
}
//创建第二行
XWPFTableRow tableRowTwo=table.createRow();
tableRowTwo.getCell(0.setText(“第一列,第二行”);
tableRowTwo.getCell(1.setText)(“第二列,第二行”);
tableRowTwo.getCell(2.setText)(“第三列,第二行”);
//创建第三行
XWPFTableRow tableRowThree=table.createRow();
tableRowThree.getCell(0.setText(“第一列,第三行”);
tableRowThree.getCell(1.setText(“第二列,第三行”);
tableRowThree.getCell(2.setText)(“第三列,第三行”);
段落=document.create段落();
write(新文件输出流(“CreateWordTable.docx”);
document.close();
System.out.println(“CreateWordTable编写成功”);
}
}
顺便说一句:请在表格前后至少写一段。否则,将光标移到表外是不可能的。如果您不提供…请提供错误捕获或异常没有错误或异常,先生..我的代码已成功运行,当我使用open office writer打开文件时,它将崩溃