Java docx文档边界外的XWPF列(Ubuntu 16.04主机)

Java docx文档边界外的XWPF列(Ubuntu 16.04主机),java,maven,apache-poi,ubuntu-16.04,libreoffice,Java,Maven,Apache Poi,Ubuntu 16.04,Libreoffice,我正在用libreofficev5.1.6.2运行一个4.10.0-42通用Ubuntu 16.04 LTS。 使用OxygenEclipse4.7.2,通过创建一个Maven项目,添加poi和POIooXML3.15作为依赖项,我尝试使用.docx格式的Java创建一个表。 不用说,我找不到解决办法。我的代码片段如下所示: public class Application { public static void main(String[] args)throws Exception {

我正在用libreofficev5.1.6.2运行一个4.10.0-42通用Ubuntu 16.04 LTS。 使用OxygenEclipse4.7.2,通过创建一个Maven项目,添加poi和POIooXML3.15作为依赖项,我尝试使用.docx格式的Java创建一个表。 不用说,我找不到解决办法。我的代码片段如下所示:

public class Application {

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

   FileOutputStream fos = new FileOutputStream("samplefile.docx");

   XWPFDocument document = new XWPFDocument();

   // New 2x2 table
    XWPFTable tableOne = document.createTable();
    XWPFTableRow tableOneRowOne = tableOne.getRow(0);
    tableOneRowOne.getCell(0).setText("Hello");
    tableOneRowOne.addNewTableCell().setText("World");

    XWPFTableRow tableOneRowTwo = tableOne.createRow();
    tableOneRowTwo.getCell(0).setText("This is");
    tableOneRowTwo.getCell(1).setText("a table");

    // Add a break between the tables
    document.createParagraph().createRun().addBreak();

    // New 3x3 table
    XWPFTable tableTwo = document.createTable();
    XWPFTableRow tableTwoRowOne = tableTwo.getRow(0);
    tableTwoRowOne.getCell(0).setText("col one, row one");
    tableTwoRowOne.addNewTableCell().setText("col two, row one");
    tableTwoRowOne.addNewTableCell().setText("col three, row one");

    XWPFTableRow tableTwoRowTwo = tableTwo.createRow();
    tableTwoRowTwo.getCell(0).setText("col one, row two");
    tableTwoRowTwo.getCell(1).setText("col two, row two");
    tableTwoRowTwo.getCell(2).setText("col three, row two");

    XWPFTableRow tableTwoRowThree = tableTwo.createRow();
    tableTwoRowThree.getCell(0).setText("col one, row three");
    tableTwoRowThree.getCell(1).setText("col two, row three");
    tableTwoRowThree.getCell(2).setText("col three, row three");

    document.write(fos);
    fos.close();
    System.out.println("Success!");
   }
}
上面的代码显示以下结果:


我想知道问题是在LibreOffice中还是在我的代码中。

当我尝试在运行Microsoft Office的虚拟Windows 10计算机上获取文件时,图形显示正确,因此我假设LibreOffice无法正确处理行,只能显示文本


问题已为我解决-下次我将在Microsoft Office和Windows下测试所有文件。

完全可以创建
*.xlsx
文件,其中包含
LibreOffice
Writer
也可以正常打开的表。但是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编写成功”);
}
}

另请参见。

没有必要放弃那么快。看看我的答案。提示:
*.docx
文件是包含
XML
文件的
ZIP
归档文件。因此,我们可以使用
LibreOffice
创建
*.docx
,然后将其解压缩,并查看
XML
,以确定
LibreOffice
如何存储内容。我使用了上面提供的代码,但似乎LibreOffice并没有将项目可视化。此外,如果我将.docx转换为.pdf格式,那么这些行将在Windows和Linux的查看器中可视化。不过,Windows、Microsoft Office中不存在此问题。由于大多数人都在使用Windows,而且没有文档在.docx中发布,而是在.pdf中发布,因此我不打算深入探究LibreOffice在内容可视化方面的神奇之处。谢谢你,@AxelRichter,谢谢你的回答!