Read excel with poi
时间: 2023-05-24 10:04:09 浏览: 56
A:To read an excel file with Apache POI, you can use the following code:
```
FileInputStream file = new FileInputStream(new File("path/to/excel/file.xlsx"));
// Create the workbook object
XSSFWorkbook workbook = new XSSFWorkbook(file);
// Get the desired sheet
XSSFSheet sheet = workbook.getSheetAt(0);
// Iterate through rows and cells
for (Row row : sheet) {
for (Cell cell : row) {
// Get the cell value
switch (cell.getCellTypeEnum()) {
case NUMERIC:
System.out.print(cell.getNumericCellValue() + "\t");
break;
case STRING:
System.out.print(cell.getStringCellValue() + "\t");
break;
default:
break;
}
}
System.out.println();
}
// Close the workbook and input stream
workbook.close();
file.close();
```
This code reads the first sheet of an Excel file and prints out the values of each cell. Note that you will need to handle exceptions appropriately, such as when the file path is invalid or the sheet does not exist.