如何在Apache POI中创建XSSFTable

3

我正在尝试使用Apache POI库和Java创建包含Excel表格的Excel表,但无法生成Microsoft Excel 2016(Office 365)可读取的结果文件。这是我的代码:

import org.apache.poi.ss.util.AreaReference;
import org.apache.poi.ss.util.CellReference;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

class Scratch {
    public static void main(String[] args) throws IOException {
        XSSFWorkbook workbook = new XSSFWorkbook();
        XSSFSheet sheet = workbook.createSheet("Table Sheet");

        XSSFRow row0 = sheet.createRow(0);
        row0.createCell(0).setCellValue("#");
        row0.createCell(1).setCellValue("Name");

        XSSFRow row1 = sheet.createRow(1);
        row1.createCell(0).setCellValue("1");
        row1.createCell(1).setCellValue("Foo");

        XSSFRow row2 = sheet.createRow(2);
        row2.createCell(0).setCellValue("2");
        row2.createCell(1).setCellValue("Bar");

        AreaReference area = workbook.getCreationHelper().createAreaReference(
                new CellReference(row0.getCell(0)),
                new CellReference(row2.getCell(1))
        );
        sheet.createTable(area);

        try(FileOutputStream file = new FileOutputStream(new File("workbook.xlsx"))) {
            workbook.write(file);
        }
    }
}

代码运行正常,但是当我在Excel中打开输出文件时,会弹出一个文件包含不可读内容的提示信息。
我已经尝试运行官方样例,结果也是一样的。官方样例可以在这里找到:https://svn.apache.org/repos/asf/poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/CreateTable.java 我需要知道最小化代码的要求,以获得可读的表格。
我正在使用Windows 10上的Oracle JavaSE JDK 1.8.0_172和Apache POI的4.0.0版本。
1个回答

6

我不确定“官方示例”代码的情况,它们似乎甚至没有经过测试。

CreateTable 使用 XSSFTable table = sheet.createTable(reference); 创建一个具有来自区域引用的 3 列的表格。但是所有这些列都具有 ID 1,所以我们需要修复。当然,这些列不应该再次创建。

因此,修复后的示例代码如下:

import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.AreaReference;
import org.apache.poi.ss.util.CellReference;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFTable;
import org.apache.poi.xssf.usermodel.XSSFTableStyleInfo;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

/**
 * Demonstrates how to create a simple table using Apache POI.
 */
public class CreateTable {

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

        try (Workbook wb = new XSSFWorkbook()) {
            XSSFSheet sheet = (XSSFSheet) wb.createSheet();

            // Set which area the table should be placed in
            AreaReference reference = wb.getCreationHelper().createAreaReference(
                    new CellReference(0, 0), new CellReference(2, 2));

            // Create
            XSSFTable table = sheet.createTable(reference); //creates a table having 3 columns as of area reference
            // but all of those have id 1, so we need repairing
            table.getCTTable().getTableColumns().getTableColumnArray(1).setId(2);
            table.getCTTable().getTableColumns().getTableColumnArray(2).setId(3);

            table.setName("Test");
            table.setDisplayName("Test_Table");

            // For now, create the initial style in a low-level way
            table.getCTTable().addNewTableStyleInfo();
            table.getCTTable().getTableStyleInfo().setName("TableStyleMedium2");

            // Style the table
            XSSFTableStyleInfo style = (XSSFTableStyleInfo) table.getStyle();
            style.setName("TableStyleMedium2");
            style.setShowColumnStripes(false);
            style.setShowRowStripes(true);
            style.setFirstColumn(false);
            style.setLastColumn(false);
            style.setShowRowStripes(true);
            style.setShowColumnStripes(true);

            // Set the values for the table
            XSSFRow row;
            XSSFCell cell;
            for (int i = 0; i < 3; i++) {
                // Create row
                row = sheet.createRow(i);
                for (int j = 0; j < 3; j++) {
                    // Create cell
                    cell = row.createCell(j);
                    if (i == 0) {
                        cell.setCellValue("Column" + (j + 1));
                    } else {
                        cell.setCellValue((i + 1.0) * (j + 1.0));
                    }
                }
            }

            // Save
            try (FileOutputStream fileOut = new FileOutputStream("ooxml-table.xlsx")) {
                wb.write(fileOut);
            }
        }
    }
}

顺便说一下: 我在如何使用apache java poi向ms excel中插入表格的代码也适用于apache poi 4.0.0sheet.createTable()已经被弃用,因此请改用XSSFTable table = sheet.createTable(null);。因为区域以及所有其他内容都是使用低级类设置的。与新示例相比,甚至没有更多的代码。


它对我不起作用。它只打印了一个有3列和2行的简单表格。有什么建议吗? - firegloves
或许是我没有看到打开列上下文菜单的箭头,而且对我来说它似乎不是一个表格... - firegloves
2
@firegloves: 这是一个表格,但没有自动筛选。可以使用 table.getCTTable().addNewAutoFilter().setRef(table.getArea().formatAsString());进行设置。 - Axel Richter

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接