```java

import java.io.FileInputStream;

jsp怎么读取Excel的内容实例,JSP如何读取Excel文件内容的示例  第1张

import java.io.IOException;

import java.util.HashMap;

import java.util.Map;

import org.apache.poi.ss.usermodel.*;

import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ExcelReaderExample {

public static Map> readExcel(String filePath) throws IOException {

Map> data = new HashMap<>();

FileInputStream excelFile = new FileInputStream(filePath);

Workbook workbook = new XSSFWorkbook(excelFile);

Sheet sheet = workbook.getSheetAt(0);

for (Row row : sheet) {

Map rowData = new HashMap<>();

for (Cell cell : row) {

rowData.put(cell.getColumnIndex(), getCellValue(cell));

}

data.put(row.getRowNum(), rowData);

}

excelFile.close();

return data;

}

private static String getCellValue(Cell cell) {

switch (cell.getCellType()) {

case STRING:

return cell.getStringCellValue();

case NUMERIC:

if (DateUtil.isCellDateFormatted(cell)) {

return cell.getDateCellValue().toString();

} else {

return Double.toString(cell.getNumericCellValue());

}

case BOOLEAN:

return Boolean.toString(cell.getBooleanCellValue());

case FORMULA:

return cell.getCellFormula();

default:

return "