gpt4 book ai didi

java - 从excel文件中读取数据,出现错误

转载 作者:行者123 更新时间:2023-11-30 05:25:35 26 4
gpt4 key购买 nike

我正在读取 Excel 文件并将其存储在 map 中以供进一步使用。使用下面的代码,运行完美。但对于某些文件,打开时会显示以下错误 enter image description here

代码:

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 并将其返回以供以后使用。我正在使用:

  • poi-3.13.jar
  • java 1.7

编辑1:添加了我完成的方法,希望它有助于重现问题。

编辑2:在错误弹出消息上单击"is"并保存后,程序运行正常。

有什么办法可以解决这个问题吗?请推荐

最佳答案

对于上面提到的文件,我使用下面的代码片段来读取文件,它返回 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

}

}

希望这对某人有帮助。

关于java - 从excel文件中读取数据,出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58692222/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com