简单的导出案例,案例使用了Apache POI库来实现Excel导出功能。最后接口会输出流给前端
不说了,直接上代码
//excel导出
public boolean expAdviceExcel(HttpServletRequest request, JSONObject form, HttpServletResponse response) throws IOException {
try {
Date date = new Date();
SimpleDateFormat time = new SimpleDateFormat("yyyy-MM-dd");
response.reset();
response.setContentType("APPLICATION/OCTET-STREAM");
// 设置http响应头信息,告诉浏览器这个文件的名字和类型
response.setHeader("Content-Disposition", "attachment; filename=" + java.net.URLEncoder.encode(time.format(date) + "意见反馈.xls","UTF-8"));
//获取数据
List<AdviceResult> list = this.downExcel(request, form, response);
HSSFWorkbook workbook = new HSSFWorkbook();
int rowNum = 0;
HSSFSheet hssfSheet = workbook.createSheet(time.format(date) + "意见反馈");
Row row = hssfSheet.createRow(rowNum++);
//Excle每列的标题
row.createCell(0).setCellValue("用户名称");
row.createCell(1).setCellValue("联系方式");
row.createCell(2).setCellValue("操作菜单");
row.createCell(3).setCellValue("操作时间");
row.createCell(4).setCellValue("是否满意");
row.createCell(5).setCellValue("反馈意见");
if (list != null && list.size() > 0) {
for (AdviceResult adviceResult : list) {
Row row1 = hssfSheet.createRow(rowNum++);
row1.createCell(0).setCellValue(adviceResult.getUsername());
row1.createCell(1).setCellValue(adviceResult.getTele());
row1.createCell(2).setCellValue(adviceResult.getMenu());
row1.createCell(3).setCellValue(adviceResult.getCreatetime());
row1.createCell(4).setCellValue(adviceResult.getIfSatisfied());
row1.createCell(5).setCellValue(adviceResult.getResultcontent());
}
}
workbook.write(response.getOutputStream());//保存Excel文件
response.getOutputStream().flush();
response.getOutputStream().close();
workbook.close();
} catch (Exception e) {
}
return true;
}