简体   繁体   English

在Java中使用POI罐读取xlsx文件时获取异常

[英]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.

相关问题 使用POI读取excel文件(xlsx)时出现java.lang.outofmemory异常 - java.lang.outofmemory exception while reading excel file (xlsx) using POI 使用POI读取.xlsm和.xlsx时出现异常 - Getting exception in reading .xlsm and .xlsx using POI Apache POI 在 Linux 环境下读取 xlsx 文件时抛出异常 - Apache POI throws exception while reading xlsx file in Linux enviroment 线程“ main”中的异常org.apache.poi.POIXMLException使用apache POI从xlsx文件读取数据时出现异常 - Exception in thread “main” org.apache.poi.POIXMLException exception appearing while reading data from xlsx file using apache POI 通过apche poi读取xlsx文件时出错 - Getting error while reading xlsx file through apche poi 使用Java和Apache poi从.xls或.xlsx文件中读取内容 - Reading contents from .xls or .xlsx file using Java and Apache poi 使用 Apache POI 读取 xlsx 文件时出现异常(org.apache.poi.openxml4j.exception - 无内容类型 [M1.13])? - Getting Exception(org.apache.poi.openxml4j.exception - no content type [M1.13]) when reading xlsx file using Apache POI? Apache POI 3.16-使用XSSF读取75​​,000行EXCEL(.xlsx)时出现OutOfMemory异常 - Apache POI 3.16 - OutOfMemory Exception while reading 75K rows EXCEL (.xlsx) using XSSF 使用Apache POI读取xlsx文件时出现异常(org.apache.poi.openxml4j.exceptions.InvalidFormatException:日期格式不正确,…)? - Exception(org.apache.poi.openxml4j.exceptions.InvalidFormatException: Date not well formated, …) when reading xlsx file using Apache POI? 对XLSX Apache poi使用Java临时文件 - using Java temporary file for XLSX Apache poi
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM