Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/342.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/26.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 Apache POI-在MS Excel中向工作簿的所有工作表中添加表时抛出错误_Java_Excel_Apache Poi - Fatal编程技术网

Java Apache POI-在MS Excel中向工作簿的所有工作表中添加表时抛出错误

Java Apache POI-在MS Excel中向工作簿的所有工作表中添加表时抛出错误,java,excel,apache-poi,Java,Excel,Apache Poi,我正在使用ApachePOI3.17和XSSFWorkbook创建excel工作簿。 工作簿包含多个工作表,我试图在每个工作表的一个表中添加数据 Java主方法 XSSFWorkbook workbook = new XSSFWorkbook(); for (String sheet : sheets) { XSSFSheet sheet = workbook.createSheet(sheet); List<String> c

我正在使用ApachePOI3.17和XSSFWorkbook创建excel工作簿。 工作簿包含多个工作表,我试图在每个工作表的一个表中添加数据

Java主方法

    XSSFWorkbook workbook = new XSSFWorkbook();
    for (String sheet : sheets)
    {
        XSSFSheet sheet = workbook.createSheet(sheet);
        List<String> colNames = Arrays.asList("Column1","Column2","Column3");

        createTable(sheet,colNames);

        for (int r = 0; r <= 1; r++)
        {
            XSSFRow row = sheet.createRow(r);
            for (int c = 0; c < colNames.size(); c++)
            {
                XSSFCell cell = row.createCell(c);
                cell.setCellValue("some value");
            }

        }
        for (int i = 0; i < colNames.size(); i++)
        {
            sheet.autoSizeColumn(i);
        }
    }
在工作表中添加表的方法

private void createTable(XSSFSheet sheet, List<String> colNames)
{
    XSSFTable table = sheet.createTable();
    CTTable cttable = table.getCTTable();

    CellReference startCellReference = new CellReference(0, 0);

    CellReference endCellReference = new CellReference(2,colNames.size());
    AreaReference areaReference = new AreaReference(startCellReference, endCellReference, SpreadsheetVersion.EXCEL2007);

    cttable.setDisplayName("SummaryData_" + sheet.getSheetName());
    cttable.setId(1);
    cttable.setName("SummaryData_" + sheet.getSheetName());
    cttable.setRef(areaReference.formatAsString());
    cttable.setTotalsRowShown(false);

    CTTableStyleInfo styleInfo = cttable.addNewTableStyleInfo();
    styleInfo.setName("TableStyleMedium13");
    styleInfo.setShowColumnStripes(false);
    styleInfo.setShowRowStripes(true);

    CTTableColumns columns = cttable.addNewTableColumns();
    columns.setCount(colNames.size());
    for (int i = 1; i <= colNames.size(); i++)
    {
        CTTableColumn column = columns.addNewTableColumn();
        column.setId(i);
        column.setName(colNames.get(i - 1));
    }
}
如果只有一张工作表,则excel会在MS excel中正确打开,但如果有多张工作表,则excel会在打开工作簿时出错

如果单击“是”,则会得到表的修复摘要

尽管工作簿中存在数据

如果我删除表并将数据直接添加到工作表中,那么它将正常工作,不会出现任何错误

有人能帮忙吗
提前感谢

这里有多个问题:

变量工作表不能同时是字符串和XSSFSheet

colNames.size在您的示例中为3。所以endCellReference是D3,areaReference是A1:D3,因此一列太宽了。它必须是A1:C3。所以:CellReference endCellReference=newCellReference2,colNames.size-1

不要使用cttable.setId1手动将所有表的ID设置为1。每个表都需要自己的Id。sheet.createTable已经正确地完成了这项工作

工作表的单元格值必须与表的列名匹配。因此,不能在所有单元格中设置某些值。显示表列名的单元格的值必须与表列名匹配

完整示例已更正:

import java.io.FileOutputStream;

import org.apache.poi.ss.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.*;
import org.apache.poi.xssf.usermodel.*;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.*;

import java.util.*;

public class CreateTablePOI3 {

 private static void createTable(XSSFSheet sheet, List<String> colNames) {

  XSSFTable table = sheet.createTable();
  CTTable cttable = table.getCTTable();

  CellReference startCellReference = new CellReference(0, 0);
  //CellReference endCellReference = new CellReference(2, colNames.size()); //one column too wide
  CellReference endCellReference = new CellReference(2, colNames.size()-1);
  AreaReference areaReference = new AreaReference(startCellReference, endCellReference, SpreadsheetVersion.EXCEL2007);

  cttable.setDisplayName("SummaryData_" + sheet.getSheetName());
  //cttable.setId(1); // Don't set table's Id manually. The sheet.createTable() is doing that properly.
  cttable.setName("SummaryData_" + sheet.getSheetName());
  cttable.setRef(areaReference.formatAsString());
  cttable.setTotalsRowShown(false);

  CTTableStyleInfo styleInfo = cttable.addNewTableStyleInfo();
  styleInfo.setName("TableStyleMedium13");
  styleInfo.setShowColumnStripes(false);
  styleInfo.setShowRowStripes(true);

  CTTableColumns columns = cttable.addNewTableColumns();
  columns.setCount(colNames.size());
  for (int i = 1; i <= colNames.size(); i++) {
   CTTableColumn column = columns.addNewTableColumn();
   column.setId(i);
   column.setName(colNames.get(i - 1));
  }
 }

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

  List<String> sheetNames = Arrays.asList("Sheet1","Sheet2","Sheet3");

  XSSFWorkbook workbook = new XSSFWorkbook();
  for (String sheetName : sheetNames) {
   XSSFSheet sheet = workbook.createSheet(sheetName);
   List<String> colNames = Arrays.asList("Column1","Column2","Column3");

   createTable(sheet, colNames);

   for (int r = 0; r <= 2; r++) {
    XSSFRow row = sheet.createRow(r);
    for (int c = 0; c < colNames.size(); c++) {
     XSSFCell cell = row.createCell(c);
     //cell.setCellValue("some value"); //sheet's cell values must match the table's column names  
     if (r == 0) {
      cell.setCellValue(colNames.get(c));
     } else {
      cell.setCellValue("some value");
     }
    }
   }
   for (int i = 0; i < colNames.size(); i++) {
    sheet.autoSizeColumn(i);
   }
  }

  FileOutputStream out = new FileOutputStream("./Excel.xlsx");
  workbook.write(out);
  out.close();
  workbook.close();
 }
}

为什么要使用Apache POI的旧版本?当您升级到最新的稳定支持版本时会发生什么?很抱歉,现在我无法升级该版本,因为它的旧代码,我只是向itI添加了新功能。我已经修改了原始代码,将其发布到此处,但没有使用相同的变量名称表和单元格引用大小等错误。但是我正在使用setId,将删除它并再次测试它,谢谢:仍然不工作,从表中删除了setId,使用第一行设置列名。Excel仍在给出错误,单击“是”时,它表示数据已恢复,并且完整的数据存在于工作表中。它仅适用于1张工作表,不知道多张工作表有什么问题:@Shashank Gupta:我的回答显示了一个完整的示例,该示例创建了一个Excel.xlsx,其中有三张工作表,每张工作表包含一个表。它经过测试并正常工作,Excel能够毫无问题地打开该文件。