How to read and write excel file

后端 未结 22 3009
北荒
北荒 2020-11-22 04:49

I want to read and write an Excel file from Java with 3 columns and N rows, printing one string in each cell. Can anyone give me simple code snippet for this? Do I need to

22条回答
  •  甜味超标
    2020-11-22 05:10

    When using the apache poi 4.1.2. The celltype changes a bit. Below is an example

        try {
            File excel = new File("/home/name/Downloads/bb.xlsx");
            FileInputStream fis = new FileInputStream(excel);
            XSSFWorkbook book = new XSSFWorkbook(fis);
            XSSFSheet sheet = book.getSheetAt(0);
    
            Iterator itr = sheet.iterator();
    
            // Iterating over Excel file in Java
            while (itr.hasNext()) {
                Row row = itr.next();
    
                Iterator 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:
    
    
                    }
                }
                System.out.println("");}
            }catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
            }
    

提交回复
热议问题