文件导出
public void downTemplateFile(String code, HttpServletResponse resp) {
InputStream iStream = null;
OutputStream outStrem = null;
try {
ImpExcelTmpl tmplFile = impExcelTmplService.getExcelTmplByCodeAndOrgRegister(code);
String url = tmplFile.getFile_url();
iStream = new FileInputStream(url);
String fileName = tmplFile.getFile_name();
String suffix = url.substring(url.lastIndexOf("."));
fileName = new String((fileName + suffix).getBytes(), "ISO8859-1");
resp.setContentType("multipart/form-data");
resp.setCharacterEncoding("utf-8");
resp.setHeader("Content-Disposition", "attachment;filename=\"" + fileName + "\"");
outStrem = resp.getOutputStream();
outStrem.write(StreamUtils.getBytes(iStream));
outStrem.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (iStream != null) {
try {
iStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (outStrem != null) {
try {
outStrem.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
excel 导出
private void downLoadExcel(String fileName, HttpServletResponse response, Workbook workbook) throws IOException {
try {
response.setCharacterEncoding("UTF-8");
response.setHeader("content-Type" , "application/vnd.ms-excel");
response.setHeader("Content-Disposition" , "attachment;filename=" + URLEncoder.encode(fileName + ".xlsx" , "UTF-8"));
workbook.write(response.getOutputStream());
} catch (Exception e) {
throw new IOException(e.getMessage());
}
}