马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
x
采用
try (Writer writer = new OutputStreamWriter(os, “UTF-8”))
UTF-8 内容转换
- public static void setResponseHeader(HttpServletResponse response, String fileName) {
- try {
- // fileName = "中文.xls";
- try {
- fileName = new String(fileName.getBytes(),"ISO8859-1");
- } catch (UnsupportedEncodingException e) {
- e.printStackTrace();
- }
- response.setContentType("application/octet-stream;charset=UTF-8");
- //response.setCharacterEncoding("UTF-8");
- response.setHeader("Content-Disposition", "attachment;filename="+ fileName);//
- response.addHeader("Pargam", "no-cache");
- response.addHeader("Cache-Control", "no-cache");
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }
- }
- public void xml2XmlOutFromResources(HttpServletResponse response, Map<String, Object> dataMap, String templetFile, String fileName) throws IOException, TemplateException {
- //设置响应头
- ExportExcel.setResponseHeader(response, fileName);
- Configuration configuration = new Configuration(Configuration.VERSION_2_3_28);
- configuration.setDefaultEncoding("UTF-8");
- configuration.setClassLoaderForTemplateLoading(this.getClass().getClassLoader(), "");
- Template template = null;
- try {
- template = configuration.getTemplate("templates/exportTemplate.xml","UTF-8");
- } catch (Exception e) {
- e.printStackTrace();
- }
- //模板和数据模型合并生成文件
- OutputStream os = null;
- try {
- os = response.getOutputStream();
- try (Writer writer = new OutputStreamWriter(os, "UTF-8")) {
- template.process(dataMap, writer);
- }
- } catch (Exception ex) {
- } finally {
- try {
- if (os != null) {
- os.close();
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
复制代码 免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |