public class ReadExcelUtils {
private static Logger logger = LoggerFactory.getLogger(ReadExcelUtils.class);
public static Object [][] readExcel(String fileName, String sheetName) {
File file = new File(fileName);
if (!file.exists()) {
logger.info("文件不存在");
return null;
}
try {
Workbook workbook = WorkbookFactory.create(file);
Sheet sheet = workbook.getSheet(sheetName);
if (sheet == null) {
logger.info("sheet名称不存在");
return null;
}
int totalRowNum = sheet.getLastRowNum();
int totalCellNum = sheet.getRow(totalRowNum).getLastCellNum();
Object [][] excelData = new Object[totalRowNum][totalCellNum];
for (int currentRowNum = 1; currentRowNum <= totalRowNum; currentRowNum++) {
int lastCellNum = sheet.getRow(currentRowNum).getLastCellNum();
for (int currentCellNUm = 0; currentCellNUm < lastCellNum; currentCellNUm++) {
excelData[currentRowNum-1][currentCellNUm] = sheet.getRow(currentRowNum).getCell(currentCellNUm);
}
}
return excelData;
} catch (IOException e) {
e.printStackTrace();
} catch (InvalidFormatException e) {
e.printStackTrace();
}
return null;
}
java:读取Excel公共类
最新推荐文章于 2022-02-06 15:54:58 发布