小编Mik*_*ike的帖子

Apache POI 合并 Word 文档中表格中的单元格

我需要有一个表格,其中第一行和第二行的单元格合并。

像这样的东西:

桌子图片(我不能发图片)http://i.stack.imgur.com/dAO6j.png

我一直在查看与此主题相关的所有问题,并且找到了将网格跨度应用于单元格的一些答案,但我找不到真正的解决方案。

这是我从谷歌和本网站获得的示例中的代码:

    XWPFDocument document = new XWPFDocument();
    XWPFTable table = document.createTable(7, 2);

    fillTable(table);

    XWPFTableCell cellRow1 = table.getRow(0).getCell(0);
    XWPFTableCell cellRow2 = table.getRow(1).getCell(0);

    cellRow1.getCTTc().addNewTcPr();
    cellRow1.getCTTc().getTcPr().addNewGridSpan();
    cellRow1.getCTTc().getTcPr().getGridSpan().setVal(BigInteger.valueOf(2L));

    cellRow2.getCTTc().addNewTcPr();
    cellRow2.getCTTc().getTcPr().addNewGridSpan();
    cellRow2.getCTTc().getTcPr().getGridSpan().setVal(BigInteger.valueOf(2L));

    FileOutputStream out = new FileOutputStream("Table.docx");
    doc.write(out);
    out.close();
Run Code Online (Sandbox Code Playgroud)

我从这段代码中得到的内容如下:

在此处输入图片说明

我试图删除“额外”的单元格,table.getRow(0).removeCell(1);但没有用,我做错了什么吗?

java apache merge cells apache-poi

5
推荐指数
1
解决办法
8507
查看次数

java.io.EOFException:使用 Apache POI 的 ZLIB 输入流意外结束

我尝试使用 Apache POI 创建 Excel 数据透视表。
目前,当我尝试在工作簿中写入数据时,出现workbook.write(fileOut);异常

org.apache.poi.ooxml.POIXMLException:java.io.EOFException:ZLIB 输入流意外结束

有类的代码:

public class PivotTable {



public static void createPivotTable(String pathToWorkbook, String sheetName) throws IOException {

    Workbook workbook = new XSSFWorkbook(pathToWorkbook);

    XSSFSheet sheet = (XSSFSheet) workbook.getSheet(sheetName);

    int firstRowInd = sheet.getFirstRowNum();
    int lastRowInd = sheet.getLastRowNum();
    int firstCellInd = sheet.getRow(0).getFirstCellNum();
    int lastCellInd = sheet.getRow(0).getLastCellNum() - 1;

    //Specifying top left ant the bottom right of the table data
    CellReference topLeft = new CellReference(firstRowInd, firstCellInd);
    CellReference botRight = new CellReference(lastRowInd, lastCellInd);

    //The area of …
Run Code Online (Sandbox Code Playgroud)

java excel pivot-table exception apache-poi

5
推荐指数
2
解决办法
1万
查看次数

标签 统计

apache-poi ×2

java ×2

apache ×1

cells ×1

excel ×1

exception ×1

merge ×1

pivot-table ×1