java export xls file by HSSFWorkbook

Use swgger2, Restlet interfaces tools bug led to the export fails, test directly using the browser
// export list - New
@UserRoleJudgment (authpos = SystemControllerLog.AUTH_PARAM, ParaName = "auth")
@SystemControllerLog (Module = "management", description = "export list", authpos = SystemControllerLog.AUTH_PARAM, ParaName = "auth")
@ RequestMapping (value = "/ exportContractExcelN", Method, = RequestMethod.GET)
@ResponseBody
@ApiOperation (value = "export list", httpMethod = "GET" , notes = "export list, you need the authorization")
public ReponseResult <Boolean> exportContractExcelN (
@ApiParam (value = "number parametric form 7564cb720a0000151bcbee13209899a0,75522b6b0a000015160873e6fa5dec29", required = to true) @RequestParam String [] IDS,
the HttpServletRequest Request,
HttpServletResponse response) throws IOException {
IF (IDS ids.length == == null || 0 || the auth == null) {
return ReponseResult.error (CodeMsg.PARAMETER_ISNULL);
}
List <String> = IDSN the java.util.Arrays.asList (IDS);
List <ContractModel> List = null;
the try {
List = contractService.exportDataList1 (IDSN);
} the catch (Exception E) {
e.printStackTrace ();
}
IF (List list.size == null || () == 0) {
return ReponseResult.error (new new CodeMsg (-1, "list is empty!"));
}
// derived form
HSSFWorkbook contractService.exportBatch1 WB = (list);
the OutputStream OS = null;
the try {

// create a common output stream
= response.getOutputStream os ();
String fileName = "file.xls";
// request to open a browser window download
response.reset ();
response.setCharacterEncoding ( "UTF-8");
// Content-Disposition tells the browser to download the open form
String header = request.getHeader ( "the User-Agent") toUpperCase ();.
IF ( header.contains ( "MSIE") || header.contains ( "TRIDENT") || header.contains ( "EDGE")) {
fileName = the URLEncoder.encode (fileName, "UTF-. 8");
fileName = fileName.replace ( "+", "% 20 "); // IE download file name space problem becomes the +
} the else {
fileName = new new String (fileName.getBytes (), "ISO8859-1");
}
response.setHeader ( "the Content -Disposition "," attachment; filename = "+ fileName); // file name to save
the response.setContentType (" file application / vnd.ms-Excel ");

// directly buffer array output stream
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);
}

Guess you like

Origin www.cnblogs.com/guangxiang/p/11429237.html