Apache POI XSSF reading in excel files

前端 未结 8 1721
粉色の甜心
粉色の甜心 2021-02-13 00:12

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:27

    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:32

    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)
  • 2021-02-13 00:32

    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:33
    public class ExcelReader{
       public String path;
       public static FileInputStream fis;
    
       public ExcelReader(){
          System.out.println("hello");
       }
    
       public ExcelReader(String path){
          this.path=path;
          fis=new FileInputStream(path);
          XSSFWorkbook workbook=new XSSFWorkbook(fis);
          XSSFSheet sheet=workbook.getSheet("Sheet1");//name of the sheet
          System.out.println(sheet.getSheetName());
          System.out.println(sheet.getLastRowNum());
          System.out.println(sheet.getRow(2).getCell(3));
      }
      public static void main(String[] args) throws IOException {
          ExcelReader excel=new ExcelReader("path of xlsx");
      }
    }
    
    0 讨论(0)
  • 2021-02-13 00:37

    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:39

    You can try the following.

    private static void readXLSX(String path) throws IOException {
        File myFile = new File(path);
        FileInputStream fis = new FileInputStream(myFile);
    
        // Finds the workbook instance for XLSX file
        XSSFWorkbook myWorkBook = new XSSFWorkbook (fis);
    
        // Return first sheet from the XLSX workbook
        XSSFSheet mySheet = myWorkBook.getSheetAt(0);
    
        // Get iterator to all the rows in current sheet
        Iterator<Row> rowIterator = mySheet.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 Cell.CELL_TYPE_STRING:
                    System.out.print(cell.getStringCellValue() + "\t");
                    break;
                case Cell.CELL_TYPE_NUMERIC:
                    System.out.print(cell.getNumericCellValue() + "\t");
                    break;
                case Cell.CELL_TYPE_BOOLEAN:
                    System.out.print(cell.getBooleanCellValue() + "\t");
                    break;
                default :
    
                }
            }
            System.out.println("");
        }
    }
    
    0 讨论(0)
提交回复
热议问题