java通过HSSFWorkbook导出xls文件
使用swgger2、Restlet等接口工具有bug导致导出失败,测试直接使用浏览器
//导出列表-新
@UserRoleJudgment(authpos = SystemControllerLog.AUTH_PARAM, paraname = "auth")
@SystemControllerLog(module = "管理", description = "导出列表",authpos = SystemControllerLog.AUTH_PARAM, paraname = "auth")
@RequestMapping(value = "/exportContractExcelN", method = RequestMethod.GET)
@ResponseBody
@ApiOperation(value = "导出列表", httpMethod = "GET", notes = "导出列表,需要授权")
public ReponseResult<Boolean> exportContractExcelN(
@ApiParam(value = "编号,参数形式7564cb720a0000151bcbee13209899a0,75522b6b0a000015160873e6fa5dec29", required = true) @RequestParam String[] ids,
HttpServletRequest request,
HttpServletResponse response) throws IOException {
if (ids == null || ids.length == 0||auth==null) {
return ReponseResult.error(CodeMsg.PARAMETER_ISNULL);
}
List<String> idsn = java.util.Arrays.asList(ids);
List<ContractModel> list = null;
try {
list = contractService.exportDataList1(idsn);
} catch (Exception e) {
e.printStackTrace();
}
if(list==null||list.size()==0){
return ReponseResult.error(new CodeMsg(-1, "列表为空!"));
}
// 导出表格
HSSFWorkbook wb = contractService.exportBatch1(list);
OutputStream os = null;
try {
// 创建一个普通输出流
os = response.getOutputStream();
String fileName = "file.xls";
// 请求浏览器打开下载窗口
response.reset();
response.setCharacterEncoding("UTF-8");
// Content-disposition 告诉浏览器以下载的形式打开
String header = request.getHeader("User-Agent").toUpperCase();
if (header.contains("MSIE") || header.contains("TRIDENT") || header.contains("EDGE")) {
fileName = URLEncoder.encode(fileName, "utf-8");
fileName = fileName.replace("+", "%20"); // IE下载文件名空格变+号问题
} else {
fileName = new String(fileName.getBytes(), "ISO8859-1");
}
response.setHeader("Content-Disposition", "attachment; filename=" + fileName);// 要保存的文件名
response.setContentType("application/vnd.ms-excel");
// 直接用数组缓冲输出流输出
wb.write(os);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return ReponseResult.success(true);
}