[英]Read data from from excel file, which is having error
我正在阅读 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:在错误弹出消息上单击是并保存后,程序运行正常。
有什么办法可以处理这个。 请建议
对于上面提到的文件,我使用下面的代码片段来读取文件,它返回StreamingReader
object。 它可以与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
}
}
希望这对某人有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.