How to read and write excel file

后端 未结 22 2586
北荒
北荒 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 04:59

    Please use Apache POI libs and try this.

        try
        {
            FileInputStream x = new FileInputStream(new File("/Users/rajesh/Documents/rajesh.xls"));
    
            //Create Workbook instance holding reference to .xlsx file
            Workbook workbook = new HSSFWorkbook(x);
    
            //Get first/desired sheet from the workbook
            Sheet sheet = workbook.getSheetAt(0);
    
            //Iterate through each rows one by one
            for (Iterator iterator = sheet.iterator(); iterator.hasNext();) {
                Row row = (Row) iterator.next();
                for (Iterator iterator2 = row.iterator(); iterator2
                        .hasNext();) {
                    Cell cell = (Cell) iterator2.next();
                    System.out.println(cell.getStringCellValue());              
                }               
            }         
            x.close();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
       }
    }
    

提交回复
热议问题