Java 导入excel获取表格信息

public ResponseFindPage<Student> findPage(@RequestParam("file") MultipartFile file) {
ResponseFindPage<Student> rfp = new ResponseFindPage<Student>();
String fileName = file.getOriginalFilename();
System.out.println(fileName);

Workbook workbook = null;
try {
InputStream in = file.getInputStream();
workbook = new XSSFWorkbook(in);
//workbook = WorkbookFactory.create(new File(String.valueOf(file)));
} catch (IOException e) {
e.printStackTrace();
}
//获取一张表
Sheet sheet = workbook.getSheetAt(0);
for (int i = 1; i <= sheet.getLastRowNum(); i++) {//跳过第一行,取得其他行数据
Row row = sheet.getRow(i);//取得第i行数据
Student student = new Student();
for (int j = 0; j < row.getLastCellNum(); j++) {
Cell cell = row.getCell(j);//取得第j列数据
cell.setCellType(CellType.STRING);
String value = cell.getStringCellValue();
System.out.print(i + " " + j + " " + value + " ");
}
if (StringUtils.isNotBlank(student.getPhone())) {
students.add(student);
}
}
rfp.setData(students);
return rfp;
}
原文地址:https://www.cnblogs.com/xiaobug/p/14550985.html