Apache POI XSSFWorkbook throwing NullPointerException for file

后端 未结 2 967
孤独总比滥情好
孤独总比滥情好 2021-01-15 13:04

I have checked every possible question on here with a similar problem, and none of the solutions have worked. I feel like I am missing something really obvious, but I just c

相关标签:
2条回答
  • 2021-01-15 13:40

    I think you have not added the jars properly.

    First go to this url : https://archive.apache.org/dist/poi/release/bin/ and download poi-bin-4.1.2-20200217.zip from there.

    Now extract the zip and add this jars to your project marked with red.

    I have tested your code and its working.

    import java.io.File;
    import java.io.IOException;
    import java.util.Iterator;
    
    import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
    import org.apache.poi.ss.usermodel.Cell;
    import org.apache.poi.ss.usermodel.Row;
    import org.apache.poi.xssf.usermodel.XSSFSheet;
    import org.apache.poi.xssf.usermodel.XSSFWorkbook;
    
    public class Testing {
        public static void main(String args[]) throws IOException, InvalidFormatException {
    
            // Linking file to Workbook
            // ****Where exception is called every time****
            XSSFWorkbook wb = new XSSFWorkbook(new File("C:\\Users\\Anish\\Downloads\\TestData.xlsx"));
    
            System.out.println("Workbook loaded");
    
            // Pulling sheet from Workbook
            XSSFSheet sheet = wb.getSheetAt(0);
    
            // Get iterator to all the rows in current sheet
            Iterator<Row> rowIterator = sheet.iterator();
    
            // Traversing over each row of XLSX file
            while (rowIterator.hasNext()) {
                Row row = rowIterator.next();
    
                // For each row, iterate through each columns
                Iterator<Cell> cellIterator = row.cellIterator();
    
                while (cellIterator.hasNext()) {
                    Cell cell = cellIterator.next();
                    switch (cell.getCellType()) {
                    case STRING:
                        System.out.print(cell.getStringCellValue() + "\t");
                        break;
                    case NUMERIC:
                        System.out.print(cell.getNumericCellValue() + "\t");
                        break;
                    case BOOLEAN:
                        System.out.print(cell.getBooleanCellValue() + "\t");
                        break;
                    default:
                    }
                }
            }
            wb.close();
    
        }
    }
    

    Output :

    Now, if you are using maven in the project.Then, you don't need to add jars on the classpath.

    Simply, add this dependency in the pom.xml.

    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi</artifactId>
        <version>4.1.2</version>
    </dependency>
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml</artifactId>
        <version>4.1.2</version>
    </dependency>
    
    0 讨论(0)
  • 2021-01-15 13:50

    So after much troubleshoooting, I abandoned using the Apache POI jar files, and switched to using maven and adding apache poi as a dependancy, and my code now runs successfully.

    Thank you too everyone who helped and made suggestions, though I am still not sure of the origin of the communication problem between Eclipse and the POI jar files.

    0 讨论(0)
提交回复
热议问题