EASYPOI导出excel
前端可使用form请求后端
(新写法可new FormData进行处理,此处未展示)
$scope.id = 0;
$scope.exportOceanTariff = function () {
var body = $('body');
var iframe = $("<iframe>");
iframe.attr('id', 'frameFile' + $scope.id);
iframe.attr('name', 'frameFile' + $scope.id);
iframe.attr('style', 'display:none');
body.append(iframe);
var form = $("<form>");
form.attr('style', 'display:none');
form.attr('target', 'frameFile' + $scope.id);
form.attr('method', 'post');
form.attr('action', '/**exportUrl**');
var input = $('<input>');
input.attr('type', 'hidden');
input.attr('name', 'oceanTariff');
input.attr('value', JSON.stringify($scope.search));
form.append(input);
body.append(form);
form.submit();
$('#frameFile' + $scope.id).load(function () {
var response = JSON.parse($(iframe[0].contentWindow.document.body).text());
if (response.success === false) {
}
})
form.remove();
$scope.id = $scope.id + 1;
}
多sheet
//添加表
List<Map<String, Object>> sheetsList = new ArrayList<>();
示例:crete sheet
/**
* create sheet
* @param sheetName sheetName
* @param dataList dataList
* @return Map《String, Object》
*/
public Map<String, Object> createSheet(String sheetName, List<ExportVo> dataList) {
if (StringUtils.isBlank(sheetName) || CollectionUtils.isEmpty(dataList)) {
return Maps.newHashMap();
}
// 准备导出模板
ExportParams exportParams = new ExportParams();
exportParams.setSheetName(sheetName);
exportParams.setHeight((short) 8);
exportParams.setStyle(ExcelExportStylerDefaultImpl.class);
Map<String, Object> map = Maps.newHashMap();
map.put("data", dataList);
map.put("entity", ExportVo.class);
map.put("title", exportParams);
return map;
}
service
public void exportData(String data, HttpServletResponse response)
sheetsList.add(createMySheet)
将自定义sheetsList整理完毕后写入response即可
String fileName = "mySheet.xls";
fileName = URLEncoder.encode(fileName, "UTF-8");
// 设置响应输出的头类型
// 设置contentType与fileName编码一致
response.setContentType("application/vnd.ms-excel;chartset=utf-8");
response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
//创建excel文件
Workbook workbook = ExcelExportUtil.exportExcel(sheetsList, ExcelType.HSSF);
//通过response输出流写入response
ServletOutputStream outputStream = response.getOutputStream();
workbook.write(outputStream);
outputStream.flush();
outputStream.close();