不同格式的文件响应不同的链接类型.
XLSX文件格式的下载
("application/ ");
public static String export2Web4File(HttpServletResponse response, String path, String excelName) throws UnsupportedEncodingException {
File file = new File((excelName).concat(()));
if (!()) {
return "文件不存在!";
}
("application/");
("utf-8");
// 这里可以防止中文乱码
excelName = (excelName, "UTF-8");
excelName = new String(new String(excelName + ()).getBytes(), "ISO8859-1");
("Content-disposition", "attachment;filename=" + excelName);
("Access-Control-Expose-Headers","Content-disposition");
try (
FileInputStream in = new FileInputStream(file);
ServletOutputStream out = ();
) {
(in, out);
return "导出成功!";
} catch (Exception e) {
("导出文件异常:", e);
}
return "导出失败!";
}
XLS文件格式的下载
("application/-excel");
public static String export2Web4FileList(HttpServletResponse response, String path, String excelName,List data) throws UnsupportedEncodingException {
File file = new File((excelName).concat(()));
if (!()) {
return "文件不存在!";
}
("application/-excel");
("utf-8");
// 这里可以防止中文乱码
excelName = (excelName, "UTF-8");
excelName = new String(new String(excelName + ()).getBytes(), "ISO8859-1");
("Content-disposition", "attachment;filename=" + excelName);
("Access-Control-Expose-Headers","Content-disposition");
try {
ExcelWriter excelWriter = (()).withTemplate(()).build();
WriteSheet writeSheet = ().build();
if ((data)) {
(data, writeSheet);
}
();
return "导出成功!";
} catch (Exception e) {
("导出文件异常:", e);
}
return "导出失败!";
}