demo:
public static void exportExcel(List<User> users){
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("sheet name");
//创建标题行
HSSFRow row = sheet.createRow(0);
row.createCell(0).setCellValue("姓名");
row.createCell(1).setCellValue("年龄");
//数据写入excel
int rowNum=1;
for(User user: users){
HSSFRow dataRow=sheet.createRow(rowNum);
dataRow.createCell(0).setCellValue(user.getName();
dataRow.createCell(1).setCellValue(user.getAge();
rowNum++;
}
//写入文件
try(FileOutputStream outputStream=new FileOutputStream("filename.xlsx"){
workbook.write(outputstream);
workbook.close();
}catch(Exception e){
log.error(e.getMessage,e);
}
}