首页 文章

在Excel文件中读取Apache POI XSSF

提问于
浏览
15

我刚才有一个关于如何使用Apache的XSSF格式读取xlsx文件的快速问题 .

现在我的代码看起来像这样:

InputStream fs = new FileInputStream(filename);   // (1)
XSSFWorkbook wb = new XSSFWorkbook(fs);           // (2)
XSSFSheet sheet = wb.getSheetAt(0);               // (3)

......导入了所有相关的东西 . 我的问题是,当我点击运行时,它会陷入第(2)行,几乎是无限循环 . filename 只是一个字符串 .

如果有人能给我一些关于如何解决这个问题的示例代码,我将非常感激 . 我现在想要的只是从xlsx文件中读取单个单元格;我使用HSSF用于xls文件并且没有问题 .

谢谢你的帮助,安德鲁

6 回答

  • 8
    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);
                }
            }
    
  • 1

    我很乐意这会回答你的问题:http://poi.apache.org/spreadsheet/quick-guide.html#ReadWriteWorkbook

    简而言之,您的代码应如下所示:

    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);
    
  • 9

    为什么要将文件分成InputStream? XSSFWorkbook有一个构造函数,它只是将路径作为String . 只需对字符串的路径进行硬编码 . 一旦创建了工作簿,就可以从中创建XSSFS表 . 然后是XSSFCells,它最终将允许您读取单个单元格的内容(单元格基于x,y位置,基本上)

  • 0

    您可以尝试以下方法 .

    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("");
        }
    }
    
  • 3

    这很好用:尝试一下

    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
    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");
      }
    }
    

相关问题