【问题标题】:Adding a sheet in an existing excel file在现有 Excel 文件中添加工作表
【发布时间】:2015-07-21 09:54:08
【问题描述】:

我尝试了多种方式来添加 Excel 电子表格。但问题总是一样的。

我尝试了这里提出的代码,

问题:

Exception in thread "main" java.lang.ClassCastException: org.apache.poi.hssf.record.BOFRecord cannot be cast to org.apache.poi.hssf.record.TabIdRecord
    at org.apache.poi.hssf.model.InternalWorkbook.fixTabIdRecord(InternalWorkbook.java:792)
    at org.apache.poi.hssf.model.InternalWorkbook.checkSheets(InternalWorkbook.java:742)
    at org.apache.poi.hssf.model.InternalWorkbook.setSheetName(InternalWorkbook.java:579)
    at org.apache.poi.hssf.usermodel.HSSFWorkbook.createSheet(HSSFWorkbook.java:748)
    at org.apache.poi.hssf.usermodel.HSSFWorkbook.createSheet(HSSFWorkbook.java:101)
    at ReadTry.main(ReadTry.java:28)

【问题讨论】:

  • 您能粘贴您的代码吗?xl 文件的详细信息是什么?你是从一个空的 xl 开始还是你已经有标签?
  • 提供stackoverflow.com/help/mcve快速解答
  • @ Frederic Henri Workbook wb = WorkbookFactory.create(new File("C:/Users/try/NewExcelFile.xls"));工作表 s = wb.createSheet(); FileOutputStream out = new FileOutputStream("C:/Users/try/NewExcelFile222.xls"); wb.write(out); out.close();

标签: java excel apache-poi


【解决方案1】:

同样的代码也适用于我

public class Modify {
   public static void main(String[] args) throws Exception {
    File f=new File("d:/temp/wb.xls");
    Workbook wb = WorkbookFactory.create(f);
    wb.createSheet("sheet2");
    FileOutputStream out = new FileOutputStream("d:/temp/wb1.xls");
    wb.write(out);
    out.close();
  }
} 

我用过poi-3.9-20121203.jarpoi-ooxml-3.9-20121203.jar

【讨论】:

  • 就我而言,此代码仅在输入文件 (d:/temp/wb.xls) 为空时才有效
  • 您的要求是什么
  • 当工作表包含数据时,我想要一种使其工作的方法
  • 我还是不明白,哪个表包含数据。当您添加新工作表时,它会变得新鲜
  • 此文件包含数据:C:/Users/try/NewExcelFile.xls。工作表有大约 500 行。我正在尝试将此工作表复制到另一个文件 xls 文件中。这将有更多工作表
猜你喜欢
  • 1970-01-01
  • 2013-06-27
  • 2018-03-22
  • 2022-08-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-25
相关资源
最近更新 更多