1

我正在尝试从 excel 文件中删除一行,但不能这样做,请参阅此处的代码

try{

    String val = request.getParameter("rdel");
    int va = 5;
    System.out.println("int val"+va);
    FileInputStream file = new FileInputStream(new File(fileName));     
    HSSFWorkbook wb = new HSSFWorkbook(file); // here exception occurs
            HSSFSheet sheet = wb.getSheetAt(0);        



        int lastRowNum=sheet.getLastRowNum();
        if(va>=0&&va<lastRowNum){
            sheet.shiftRows(va+1,lastRowNum, -1);
        }
        if(va==lastRowNum){
            HSSFRow removingRow=sheet.getRow(va);
            if(removingRow!=null){
                sheet.removeRow(removingRow);
            }
        }

    FileOutputStream out = new FileOutputStream ("D:/task.xls");
    wb.write(out);



        }catch(Exception e){ 
        e.printStackTrace(); 
}
        return SUCCESS;
}   

见异常

org.apache.poi.hssf.record.RecordFormatException: Unable to construct record instance
at org.apache.poi.hssf.record.RecordFactory.createRecord(RecordFactory.java:186)
at org.apache.poi.hssf.record.RecordFactory.createRecords(RecordFactory.java:328)
at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:271)
at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:196)
at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:312)
at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:293)
at com.struts.curd.Delete.execute(Delete.java:47)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
4

1 回答 1

1

这个问题在 3.2 之后被删除了......用最近的 POI 版本更新(现在是 3.9)......希望你不会再遇到这个问题......

详情请查看这里

于 2013-07-02T12:39:48.610 回答