[英]Getting exception while reading xlsx file using POI jars in java
I am unable to read xlsx
file using java, getting Exception
as at the line where I am creating an object XSSFWorkbook(fis); 我无法使用Java读取
xlsx
文件,在创建对象XSSFWorkbook(fis)的那一行获取Exception
;
HashMap<Short, String> keyMap = new HashMap<Short, String>();
HashMap<String, String> valueMap = null;
ArrayList<HashMap<String, String>> rowList = new ArrayList<HashMap<String, String>>();
try {
FileInputStream fis = new FileInputStream(fileName);
String fileExtn = getFileExtension(fileName);
Workbook xssfWorkBook ;
HSSFWorkbook hssfWorkBook ;
Sheet sheet=null;
if (fileExtn.equalsIgnoreCase("xlsx"))
{
log.debug("Before creating WorkBook");
xssfWorkBook = new XSSFWorkbook(fis);
log.debug("xssfWorkBook object created");
sheet = xssfWorkBook.getSheetAt(0);
}else if (fileExtn.equalsIgnoreCase("xls"))
{
hssfWorkBook = new HSSFWorkbook(fis);
sheet = (Sheet) hssfWorkBook.getSheetAt(0);
}
Iterator<Row> rows = sheet.rowIterator();
int rowCount = 0;
while (rows.hasNext()) {
if (rowCount == 0) {
Row row = rows.next();
Iterator<Cell> cells = row.cellIterator();
short cellCounter = 1;
while (cells.hasNext()) {
Cell cell = cells.next();
String cellValue = getCellValueAsString(cell);
keyMap.put(cellCounter, cellValue);
cellCounter++;
}
} else {
valueMap = new HashMap<String, String>();
Row row = rows.next();
Iterator<Cell> cells = row.cellIterator();
short cellCounter = 1;
while (cells.hasNext()) {
Cell cell = cells.next();
String cellValue = getCellValueAsString(cell);
valueMap.put(keyMap.get(cellCounter), cellValue);
cellCounter++;
}
rowList.add(valueMap);
}
rowCount++;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
log.error("FileNotFoundException" + e);
throw new Exception("FileNotFoundException while reading Excel file, Message <" + e.getMessage() + ">");
} catch (IOException e) {
e.printStackTrace();
log.error("IOException" +e);
throw new Exception("IOException while reading Excel file, Message <" + e.getMessage() + ">");
}catch(Exception e){
e.printStackTrace();
throw new Exception("Exception while reading Excel file, Message <" + e.getMessage() + ">");
}
Getting below exception as stackTrace is : 作为stackTrace获得以下异常是:
java.lang.NullPointerException at org.apache.poi.openxml4j.opc.OPCPackage.getPart(OPCPackage.java:562) at org.apache.poi.POIXMLDocumentPart.(POIXMLDocumentPart.java:63) at org.apache.poi.POIXMLDocument.(POIXMLDocument.java:58) at org.apache.poi.xssf.usermodel.XSSFWorkbook.(XSSFWorkbook.java:186) at com.covad.portlet.mars.util.ExcelReader.readExcel(ExcelReader.java:72)
位于org.apache.poi.POIXMLDocumentPart。(POIXMLDocumentPart.java:63)处org.apache.poi.openxml4j.opc.OPCPackage.getPart(OPCPackage.java:562)处的java.lang.NullPointerException 。(POIXMLDocument.java:58)(位于org.apache.poi.xssf.usermodel.XSSFWorkbook。(XSSFWorkbook.java:186)位于com.covad.portlet.mars.util.ExcelReader.readExcel(ExcelReader.java:72)
I had this issue before make sure that the file is saved as ExcelWorkbook( .xlsx) ,the issue may happen if you saved the file as Strict Open XML SpreadSheet( .xlsx),they both have the same extension but the internal structures may vary and POI seems does not support it;to solve the issue on the file re save it again as ExcelWorkbook(*.xlsx) . 在确保将文件另存为ExcelWorkbook( .xlsx)之前,我曾遇到此问题,如果将文件另存为严格打开XML SpreadSheet( .xlsx),则可能会发生此问题 ,它们的扩展名相同,但内部结构可能有所不同而且POI似乎不支持它;要解决文件上的问题,请将其重新保存为ExcelWorkbook(*。xlsx)。 I hope this may solve your issue.
我希望这可以解决您的问题。
Yes, the above programs needs few jars as poi-3.7.jar, poi-ooxml-3.7-20101029.jar, poi-ooxml-schemas-3.7-20101029.jar, xmlbeans-2.3.0.jar and dom4j-1.6.1.jar. 是的,上述程序需要几个jar,例如poi-3.7.jar,poi-ooxml-3.7-20101029.jar,poi-ooxml-schemas-3.7-20101029.jar,xmlbeans-2.3.0.jar和dom4j-1.6.1 。罐。
Also, the input file for which I was trying had some problem. 另外,我尝试为其输入文件有一些问题。 I created new blank file and copied all data which I needed and tried to read the same.
我创建了一个新的空白文件,并复制了我需要的所有数据,并尝试读取相同的数据。 It worked.
有效。
Thank you All for your time and help. 谢谢大家的时间和帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.