1. 用例
在开发系统时,一般涉及到查询统计数据,都有导出数据到Excel中并下载到客户端的功能.
2. 流程
2.1 浏览器端点击导出数据到excel.
2.2 服务器接收到该请求后,处理请求,查询数据.
2.3 服务器通过调用poi API,产生excel数据.
2.4 修改response 表头属性.
2.5 把excel数据写入response输出流中.
3. 配置及类
3.1 controller方法
//导出excel
@RequestMapping("/hello/export")
public void export(HttpServletResponse response) {
List<UserDto> list=userInfoService.findUserDtoListByKeyWord(getPagination(new SearchDto(), 1));
ExportTableDto dto=ExportTableHandler.getExportTableDtoByType("user");
try {
ExcelExportUtils.exportExcel(response,list,dto.getTitle(),dto.getMap());
} catch (IOException e) {
throw new PropertyException(e.getMessage());
}
}
3.2 excel导出工具类
public class ExcelExportUtils {
private static Logger logger = LogManager.getLogger(ExcelExportUtils.class);
/**
* 设置头信息
*
* @param response
* @param fileName
*/
private static void setResponseHeader(HttpServletResponse response, String fileName) {
response.setContentType("application/octet-stream;charset=utf-8");
try {
response.setHeader("Content-Disposition", "attachment;filename="
+ java.net.URLEncoder.encode(fileName + "_" + new SimpleDateFormat("yy