Java 从excel文件读取数据时出错

Java 从excel文件读取数据时出错,java,excel,apache-poi,Java,Excel,Apache Poi,我正在阅读excel文件并将其存储在地图中以供进一步使用。使用下面的代码,这是完美的工作。但对于某些文件,打开时显示以下错误 代码: private Workbook workbook; public Map<String,String> readMaster(Properties properties) { Map<String,String> masterMap = new HashMap<String, String>();

我正在阅读excel文件并将其存储在地图中以供进一步使用。使用下面的代码,这是完美的工作。但对于某些文件,打开时显示以下错误

代码:

private Workbook workbook;
    public Map<String,String> readMaster(Properties properties) {
    Map<String,String> masterMap = new HashMap<String, String>();
     try {
            File masterFile = new File(properties.get(ReportConstants.DCS_INPUT_PATH) + ReportConstants.DCS_MASTER_FILE);
            if(!masterFile.exists()){
                throw new FileNotFoundException();
            }else{
            FileInputStream excelFile = new FileInputStream(masterFile);
            workbook = new XSSFWorkbook(excelFile);
            Sheet datatypeSheet = workbook.getSheetAt(0);
            Iterator<Row> iterator = datatypeSheet.iterator();
            int headItr = 0;
            while (iterator.hasNext()) {
                String key="",value="";
                Row currentRow = iterator.next();
                Iterator<Cell> cellIterator = currentRow.iterator();
                int rowItr = 0;
                while (cellIterator.hasNext()) {
                    Cell cell = cellIterator.next();
                    //Some logic
}
masterMap.put(key, value);
                    }
                }

             for (Map.Entry<String,String> entry : masterMap.entrySet()){  
                    System.out.println("Key = " + entry.getKey() + 
                                     ", Value = " + entry.getValue()); 
            } 
            if(!masterMap.isEmpty()){
                /*FileUtility.checkDestinationDir(""+properties.get(ReportConstants.DCS_ARCHIVE_PATH));
                FileUtility.moveFile(properties.get(ReportConstants.DCS_INPUT_PATH) + ReportConstants.DCS_MASTER_FILE,
                        properties.get(ReportConstants.DCS_ARCHIVE_PATH)+ReportConstants.DCS_MASTER_FILE+FileUtility.getArchivedPattern());*/
            }else{
                EmailService.sendExceptionMail(properties.getProperty(ReportConstants.EMAIL_EXCEPTION_TO),
                        properties.getProperty(ReportConstants.EMAIL_EXCEPTION_CC),
                                " No data in master file ",
                                "DCS : Empty Master File");
                throw new Exception("No data in master file");
            } 

        } catch (FileNotFoundException e) {
            log.error("File not present while reading Master file ",e);
            EmailService.sendExceptionMail(properties.getProperty(ReportConstants.EMAIL_EXCEPTION_TO),
                    properties.getProperty(ReportConstants.EMAIL_EXCEPTION_CC),
                            " File not present while reading Master file \n\n"+e.getMessage(),
                            "DCS Report : Master File");
        } catch (IOException e) {
            log.error("Input/Output Exception occured while reading Master file ",e);
            EmailService.sendExceptionMail(properties.getProperty(ReportConstants.EMAIL_EXCEPTION_TO),
                    properties.getProperty(ReportConstants.EMAIL_EXCEPTION_CC),
                            " Input/Output Exception occured while reading Master file \n\n"+e.getMessage(),
                            "DCS Report : Master File");
        } catch (Exception e) {
            log.error("Genric exception occured while reading Master file ",e);
            EmailService.sendExceptionMail(properties.getProperty(ReportConstants.EMAIL_EXCEPTION_TO),
                    properties.getProperty(ReportConstants.EMAIL_EXCEPTION_CC),
                            "Exception Occurred : \n\n"+e.getMessage(),
                            "DCS Report : Master File");
        }
     finally{
         try {
                if (null!=excelFile) {
                    excelFile.close();
                }
                if(null!=workbook){
                    workbook.close();
                }
            } catch (Exception e2) {
                e2.printStackTrace();
            }



 }
         return masterMap;
    }
私人工作簿;
公共地图读取器(属性){
Map masterMap=newhashmap();
试一试{
File masterFile=新文件(properties.get(ReportConstants.DCS\u INPUT\u PATH)+ReportConstants.DCS\u MASTER\u文件);
如果(!masterFile.exists()){
抛出新的FileNotFoundException();
}否则{
FileInputStream excelFile=新的FileInputStream(主文件);
工作簿=新XSSF工作簿(Excel文件);
工作表数据类型工作表=工作簿。getSheetAt(0);
迭代器迭代器=datatypeSheet.Iterator();
int headItr=0;
while(iterator.hasNext()){
字符串键=”,值=”;
Row currentRow=iterator.next();
迭代器cellIterator=currentRow.Iterator();
int-rowItr=0;
while(cellIterator.hasNext()){
Cell=cellIterator.next();
//一些逻辑
}
主地图。放置(键、值);
}
}
对于(Map.Entry:masterMap.entrySet()){
System.out.println(“Key=“+entry.getKey()+
,Value=“+entry.getValue());
} 
如果(!masterMap.isEmpty()){
/*FileUtility.checkDestinationDir(“+properties.get(ReportConstants.DCS\u ARCHIVE\u PATH));
FileUtility.moveFile(properties.get(ReportConstants.DCS\u INPUT\u PATH)+ReportConstants.DCS\u MASTER\u文件,
get(ReportConstants.DCS_ARCHIVE_PATH)+ReportConstants.DCS_MASTER_FILE+FileUtility.getArchivedPattern())*/
}否则{
EmailService.sendExceptionMail(properties.getProperty(ReportConstants.EMAIL\u EXCEPTION\u TO),
properties.getProperty(ReportConstants.EMAIL\u EXCEPTION\u CC),
“主文件中没有数据”,
“DCS:空主文件”);
抛出新异常(“主文件中没有数据”);
} 
}catch(filenotfounde异常){
log.error(“读取主文件时文件不存在”,e);
EmailService.sendExceptionMail(properties.getProperty(ReportConstants.EMAIL\u EXCEPTION\u TO),
properties.getProperty(ReportConstants.EMAIL\u EXCEPTION\u CC),
“读取主文件时文件不存在\n\n”+e.getMessage(),
“跟单信用证报告:主文件”);
}捕获(IOE异常){
log.error(“读取主文件时发生输入/输出异常”,e);
EmailService.sendExceptionMail(properties.getProperty(ReportConstants.EMAIL\u EXCEPTION\u TO),
properties.getProperty(ReportConstants.EMAIL\u EXCEPTION\u CC),
“读取主文件时发生输入/输出异常\n\n”+e.getMessage(),
“跟单信用证报告:主文件”);
}捕获(例外e){
log.error(“读取主文件时发生Genric异常”,e);
EmailService.sendExceptionMail(properties.getProperty(ReportConstants.EMAIL\u EXCEPTION\u TO),
properties.getProperty(ReportConstants.EMAIL\u EXCEPTION\u CC),
“发生异常:\n\n”+e.getMessage(),
“跟单信用证报告:主文件”);
}
最后{
试一试{
if(null!=excelFile){
excelFile.close();
}
如果(null!=工作簿){
workbook.close();
}
}捕获(异常e2){
e2.printStackTrace();
}
}
返回主地图;
}
此外,我正在将详细信息保存到地图,并将其返回以供以后使用。 我正在使用:

  • poi-3.13.jar
  • java 1.7
编辑1:添加了我完成的方法,希望它有助于复制问题

编辑2:在错误弹出消息上单击“是”并保存后,程序工作正常


有没有办法处理这件事。请建议

对于上面提到的文件,我使用下面的代码片段来读取文件,它返回
StreamingReader
对象。它可以与
工作表
一样进一步使用

代码

import com.monitorjbl.xlsx.StreamingReader;

public static StreamingReader getExcelSheet(File excelFile, String sheetName) throws Exception{
        StreamingReader reader = null;
        reader = StreamingReader.builder()
                .rowCacheSize(100) // number of rows to keep in memory (defaults to 10)
                .bufferSize(4096) // buffer size to use when reading InputStream to file (defaults to 1024)
                .sheetName(sheetName) // index of sheet to use (defaults to 0)
                .read(excelFile);
        return reader;
    }

StreamingReader sheet1 = FileUtility.getExcelSheet(excelFile, sheetName);   
for (Row r : sheet1){

    // iterate row here

    for (Cell cell : r) {

    //iterate cells here

    }

}

希望这对您有所帮助。

您是否在代码末尾关闭
文件输入流
工作簿
?我不知道这是否重要,但这些文件可能是不同版本的Excel文件,如xls和xlsx@Sterconium所有文件都是
xlsx
“但对于某些文件,打开时显示以下错误…”如果显示该消息,则这些文件已损坏。所以一定是什么东西腐蚀了他们。他们从哪里来?这些文件是如何创建的?使用
apache poi
没有读取损坏文件的通用方法。更好的方法是避免损坏文件。如果您在该消息上单击[是],那么具体会发生什么?它应该告诉您文件的确切部分已损坏。