Apache POI XSSF reading in excel files

前端 未结 8 2082
北恋
北恋 2021-02-13 00:00

I just have a quick question about how to read in an xlsx file using the XSSF format from Apache.

Right now my code looks like this:

InputStream fs = ne         


        
相关标签:
8条回答
  • 2021-02-13 00:26

    I have the same error, I have just updated the pom dependencies with the same version. It worked.

            <!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
            <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi</artifactId>
                <version>4.1.0</version>
            </dependency>
            <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
            <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi-ooxml</artifactId>
                <version>4.1.0</version>
            </dependency>
    
    0 讨论(0)
  • 2021-02-13 00:27
    InputStream inp = null;
            try {
                inp = new FileInputStream("E:/sample_poi.xls");
    
                Workbook wb = WorkbookFactory.create(inp);
                Sheet sheet = wb.getSheetAt(0);
                Header header = sheet.getHeader();
    
                int rowsCount = sheet.getLastRowNum();
                System.out.println("Total Number of Rows: " + (rowsCount + 1));
                for (int i = 0; i <= rowsCount; i++) {
                    Row row = sheet.getRow(i);
                    int colCounts = row.getLastCellNum();
                    System.out.println("Total Number of Cols: " + colCounts);
                    for (int j = 0; j < colCounts; j++) {
                        Cell cell = row.getCell(j);
                        System.out.println("[" + i + "," + j + "]=" + cell.getStringCellValue());
                    }
                }
    
            } catch (Exception ex) {
                java.util.logging.Logger.getLogger(FieldController.class.getName()).log(Level.SEVERE, null, ex);
            } finally {
                try {
                    inp.close();
                } catch (IOException ex) {
                    java.util.logging.Logger.getLogger(FieldController.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
    
    0 讨论(0)
  • 2021-02-13 00:28

    this works fine: try it

    File filename = new File("E:/Test.xlsx");
    FileInputStream isr= new FileInputStream(filename);
    
    Workbook book1 = new XSSFWorkbook(isr);
    Sheet sheet = book1.getSheetAt(0);  
    Iterator<Row> rowItr = sheet.rowIterator();
    
    0 讨论(0)
  • 2021-02-13 00:31

    Why are you breaking the file into an InputStream? XSSFWorkbook has a constructor that simply takes the path as a String. Just hard code the path of the string in. Once you create the workbook you can create XSSFSheets from that. Then XSSFCells, which will then finally allow you to read the contents of a single cell (cells are based on x,y locations, essentially)

    0 讨论(0)
  • 2021-02-13 00:32
    // Load sheet- Here we are loading first sheet only
    XSSFSheet xlSheet = wb.getSheetAt(0);
    

    Here code recognizing only first sheet - In my Excel multiple sheets are there so facing problem to change sheet2 to sheet1(getSheetAt(0))...

    0 讨论(0)
  • 2021-02-13 00:35

    I bealive that this will answer your questions: http://poi.apache.org/spreadsheet/quick-guide.html#ReadWriteWorkbook

    In short, your code should look like this:

    InputStream inp = new FileInputStream("workbook.xlsx");
    Workbook wb = WorkbookFactory.create(inp);
    Sheet sheet = wb.getSheetAt(0);
    Row row = sheet.getRow(2);
    Cell cell = row.getCell(3);
    
    0 讨论(0)
提交回复
热议问题