java export file encoding issues

public static void exportToExcel(String uid, String name, String htmlText,
HttpServletRequest request, HttpServletResponse response) {
htmlText = htmlText.replaceFirst("<table>", "<tableFirst>");
htmlText = htmlText.replaceAll("<table>",
"<table cellpadding=\"3\" cellspacing=\"0\" border=\"1\" rull=\"all\"
style=\"border-collapse: collapse\">");
htmlText = htmlText.replaceFirst("<tableFirst>", "<table>");
try (OutputStream out = response.getOutputStream()) {
String fileName = name+ "_" + DateUtils.getNow("yyyyMMddHHmmss");
// fileName = new String(fileName.getBytes(),"utf-8")+ ".xls";
if ("large".equals(htmlText)) {
ReportingPo report = reportingService.getByUid(uid);
Map<String, Object> formParameters = generationService.getFormParameters(request.getParameterMap(),
report.getDataRange());
ReportTable reportTable = generationService.getReportTable(report, formParameters);
htmlText = reportTable.getHtmlText();
}
// response.reset();
response.addHeader("Content-Disposition", "attachment;filename=" +
new String(fileName.getBytes("utf-8"),"iso-8859-1")+ ".xls");
// response.setHeader("Content-Disposition", String.format("attachment; filename=%s", fileName));
response.setContentType("application/vnd.ms-excel; charset=utf-8");
response.setCharacterEncoding("utf-8");
response.addCookie(new Cookie("fileDownload", "true"));
// out.write(new byte[] { (byte) 0xEF, (byte) 0xBB, (byte) 0xBF }); // 生成带bom的utf8文件
out.write(htmlText.getBytes("utf-8"));
out.flush();
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}

Note that two coding:
new new String (fileName.getBytes ( "UTF-. 8"), "ISO-8859-1") + "* .xls"
----------------- ----
author: it_0101
source: CSDN
original: https: //blog.csdn.net/it_0101/article/details/80430717
copyright: This article is a blogger original article, reproduced, please attach Bowen link!

Guess you like

Origin www.cnblogs.com/yx10/p/11304249.html