java:获取excel文件表头数据和列表数据
注:导入的文件存放到单独的一张文件表种,其他表存文件表id
//一、根据fileid查询文件获取文件流
InputStream inputStream = null;
try {
inputStream = sysFileCoreFeignClient.download(fileId).body().asInputStream();
} catch (IOException e) {
e.printStackTrace();
throw new BusinessException(“获取文件流失败!”);
}
//二、读取excel文件内容
//1.获取工作薄
Workbook workbook = null;
try {
workbook = WorkbookFactory.create(inputStream);
} catch (IOException | InvalidFormatException e) {
e.printStackTrace();
throw new BusinessException(“读取excel文件内容失败!”);
}
// 2.获取sheet页
Sheet sheet = workbook.getSheetAt(0);
//2.1.从0开始;获取sheet页第一行(即表头行)
Row Row = sheet.getRow(0);
//从0开始;getCell(0)表示第一列
String head1 = Row.getCell(0).getStringCellValue()
//2.2.循环文件行,从1开始(即表格数据)
for (int rowNum = 1; rowNum <= sheet.getLastRowNum(); rowNum++) {
Row Row = sheet.getRow(rowNum);
if (Row != null) {
//判断这行记录是否存在(判断空行)
if (Row.getLastCellNum() < 1 || Row.getCell(0) == null) {
continue;
}
}
}