我正在读取excel文件并将其存储在Map中以备将来使用。使用下面的代码,这是完美的工作。但是对于某些文件,打开时显示以下错误
代码:
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并将其返回以供以后使用。 我正在使用:
编辑1:添加了我已完成的方法,希望它有助于重现此问题。
编辑2:在错误弹出消息上单击“是”并保存后,程序可以正常工作。
有什么办法可以解决这个问题。请建议
答案 0 :(得分:1)
对于上面提到的文件,我正在使用下面的代码片段读取文件,它返回StreamingReader
对象。可以与Sheet
进一步使用。
代码
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
}
}
希望这个帮助的人。