【问题标题】:Unable to Add Sheet using Apache POI ss user model无法使用 Apache POI ss 用户模型添加工作表
【发布时间】:2016-12-18 16:06:35
【问题描述】:

我正在尝试使用 Apache POI 添加工作表,但是当我尝试保存 excel 时出现空指针异常。不确定为什么会发生这种情况,但是对 excel 的数据更新非常完美。

发生异常: org.apache.poi.POIXMLException: java.lang.NullPointerException

public XL_ReadWrite(String path) throws Exception{
  this.path = path;
    try{ 
        fin = new FileInputStream(path);
        workbook = WorkbookFactory.create(fin);
        sheet = workbook.getSheetAt(0);
        fin.close();
    } catch(FileNotFoundException e){
        throw new Exception("Exception occurred while finding the file : " + path + " while XLS initialize .Exception details : "+e.getMessage());
    } catch(IOException e){
        throw new Exception("I/O interrupted exception occurred while XLS initialize .Exception details : " + e.getMessage());
    } catch(Exception e){
        throw new Exception("Exception occurred while XLS initialize .Exception details : "+ e.getMessage());
  }
}

public void addSheet(String sheetName) throws Exception{
        File file = new File(path);
        String fileExtn = Globals.GC_EMPTY;
        workbook = null;
        if (file.exists()) {
            try {
                fileExtn = FilenameUtils.getExtension(file.getAbsolutePath());
                if(fileExtn.equals("xlsx")){
                    workbook = (XSSFWorkbook) WorkbookFactory.create(file);
                }else if(fileExtn.equals("xls")){
                    workbook = (HSSFWorkbook) WorkbookFactory.create(file);
                }
                sheet = workbook.createSheet(sheetName);
            }catch (InvalidFormatException e) {
                throw new Exception("InvalidFormatException occurred while adding sheet : " + sheetName + ". Exception details : "+e.getMessage());     
            }catch(IOException e){
                throw new Exception("IOException occurred while adding sheet : " + sheetName + ". Exception details : "+e.getMessage());
            }
        }
        else{
            throw new FileNotFoundException("Error : " + path + " not found");
        }
    }

public void saveXL() throws Exception{      
        try{
            fout = new FileOutputStream(path);  
            this.workbook.write(this.fout);
            this.fout.close();
        }catch(Exception e){
            throw new Exception("Exception occurred while saving excel. Exception details :" +e.getMessage());
        }   
    }

【问题讨论】:

    标签: java excel selenium apache-poi automated-tests


    【解决方案1】:

    问题已解决

    if(fileExtn.equals("xlsx")){
                        workbook = (XSSFWorkbook) WorkbookFactory.create(new FileInputStream(path));
                    }else if(fileExtn.equals("xls")){
                        workbook = (HSSFWorkbook) WorkbookFactory.create(new FileInputStream(path));
                    }
    

    【讨论】:

    • 你为什么认为这是必要的?为什么不直接使用 WorkbookFactory 并让它自动检测?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-27
    • 1970-01-01
    • 2014-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多