Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在ApachePOI中创建XSSFTable_Java_Excel_Apache Poi - Fatal编程技术网

Java 如何在ApachePOI中创建XSSFTable

Java 如何在ApachePOI中创建XSSFTable,java,excel,apache-poi,Java,Excel,Apache Poi,我正在尝试使用Java和Apache POI库创建包含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.

我正在尝试使用Java和Apache POI库创建包含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中打开输出文件时,会收到一条消息,该文件包含无法读取的内容

我试过运行官方样本,结果是一样的。官方样本可在此处找到:

我需要知道获取可读表所需的最低代码


我在Windows 10上使用4.0.0版Apache POI和Oracle JavaSE JDK 1.8.0_172。

始终不确定“官方示例”代码会发生什么。它们似乎甚至没有经过测试

CreateTable
使用
XSSFTable table=sheet.CreateTable(参考)创建一个表,该表包含区域引用中给定的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);
            }
        }
    }
}
import java.io.FileOutputStream;
导入java.io.IOException;
导入org.apache.poi.ss.usermodel.工作簿;
导入org.apache.poi.ss.util.AreaReference;
导入org.apache.poi.ss.util.CellReference;
导入org.apache.poi.xssf.usermodel.XSSFCell;
导入org.apache.poi.xssf.usermodel.XSSFRow;
导入org.apache.poi.xssf.usermodel.xssfheet;
导入org.apache.poi.xssf.usermodel.XSSFTable;
导入org.apache.poi.xssf.usermodel.XSSFTableStyleInfo;
导入org.apache.poi.xssf.usermodel.xssf工作簿;
/**
*演示如何使用ApachePOI创建简单表。
*/
公共类CreateTable{
公共静态void main(字符串[]args)引发IOException{
尝试(工作簿wb=new XSSFWorkbook()){
XSSFSheet sheet=(XSSFSheet)wb.createSheet();
//设置桌子应放置在哪个区域
AreaReference=wb.getCreationHelper().createAreaReference(
新的CellReference(0,0),新的CellReference(2,2));
//创造
XSSFTable table=sheet.createTable(reference);//创建一个具有3列的表作为区域引用
//但所有这些都有id 1,所以我们需要修理
table.getCTTable().getTableColumns().getTableColumnArray(1.setId(2);
table.getCTTable().getTableColumns().getTableColumnArray(2.setId(3);
表.集合名称(“测试”);
table.setDisplayName(“测试表”);
//现在,以低级方式创建初始样式
table.getCTTable().addNewTableStyleInfo();
table.getCTTable().getTableStyleInfo().setName(“TableStyleMedium2”);
//摆桌子
XSSFTableStyleInfo style=(XSSFTableStyleInfo)table.getStyle();
style.setName(“TableStyleMedium2”);
样式。设置显示柱状条纹(假);
style.setShowRowStripes(真);
style.setFirstColumn(false);
style.setLastColumn(false);
style.setShowRowStripes(真);
style.setShowColumnStripes(真);
//设置表的值
XSSFRow行;
XSSFCell细胞;
对于(int i=0;i<3;i++){
//创建行
行=表。创建行(i);
对于(int j=0;j<3;j++){
//创建单元
cell=row.createCell(j);
如果(i==0){
cell.setCellValue(“列”+(j+1));
}否则{
cell.setCellValue((i+1.0)*(j+1.0));
}
}
}
//拯救
try(FileOutputStream fileOut=newfileoutputstream(“ooxml table.xlsx”)){
wb.写入(文件输出);
}
}
}
}

顺便说一句:我的代码也可以使用ApachePOI4.0.0来工作。
sheet.createTable()
已弃用。使用
XSSFTable table=sheet.createTable(null)也可以这样做取而代之。因为区域以及所有其他东西都是使用低级类设置的。虽然没有比新示例更多的代码。

它对我不起作用。它打印一张3列2行的简单表格。有什么建议吗?或者我看不到负责打开列上下文菜单的箭头,在我看来它不是一个表…@fireholds:它是一个表,但没有自动过滤器。这可以使用
table.getCTTable().addNewAutoFilter().setRef(table.getArea().formataString())设置