From 2f4cc85cd586618865ad2892fa378fff123613f9 Mon Sep 17 00:00:00 2001 From: linzhijie <1003392067@qq.com> Date: 星期五, 21 十月 2022 12:58:58 +0800 Subject: [PATCH] 更改转换方法 --- src/main/java/com/ots/framework/web/controller/BaseController.java | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 46 insertions(+), 0 deletions(-) diff --git a/src/main/java/com/ots/framework/web/controller/BaseController.java b/src/main/java/com/ots/framework/web/controller/BaseController.java index 7f88ce7..eb95146 100644 --- a/src/main/java/com/ots/framework/web/controller/BaseController.java +++ b/src/main/java/com/ots/framework/web/controller/BaseController.java @@ -1,4 +1,5 @@ package com.ots.framework.web.controller; +import cn.hutool.core.util.URLUtil; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import com.ots.common.utils.DateUtils; @@ -11,9 +12,19 @@ import com.ots.framework.web.page.TableDataInfo; import com.ots.framework.web.page.TableSupport; import com.ots.project.system.user.domain.User; +import org.apache.poi.xssf.usermodel.XSSFWorkbook; +import org.springframework.core.io.ClassPathResource; import org.springframework.web.bind.WebDataBinder; import org.springframework.web.bind.annotation.InitBinder; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; import java.beans.PropertyEditorSupport; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.UnsupportedEncodingException; +import java.nio.charset.Charset; import java.util.Date; import java.util.List; import java.util.regex.Matcher; @@ -112,4 +123,39 @@ } return json; } + + public void downDeviceManagementTemplate(String fileName, String path , HttpServletRequest req, HttpServletResponse res) throws UnsupportedEncodingException { + // 璇诲彇鏈嶅姟鍣ㄧ妯℃澘鏂囦欢 + // ClassPathResource绫荤殑鏋勯�犳柟娉曟帴鏀惰矾寰勫悕绉帮紝鑷姩鍘籧lasspath璺緞涓嬫壘鏂囦欢 + ClassPathResource classPathResource = new ClassPathResource(path); + + //鍒涘缓涓存椂鏂囦欢 + XSSFWorkbook workbook = null; + try (InputStream inputStream = classPathResource.getInputStream()) { + // 杞负POI鐨刉orkbook杈撳嚭娴侊紝杩欐牱涓嬭浇鍚� 鎵撳紑涓嶆姤閿欙紝鐩存帴杈撳嚭 娴� 鐨勮瘽锛屾墦寮�excel鍙兘鎻愮ず閿欒 + workbook = new XSSFWorkbook(inputStream); + } catch (IOException e) { + e.printStackTrace(); + throw new RuntimeException("妯℃澘涓嬭浇寮傚父"); + } + String userAgent = req.getHeader("User-Agent"); + // 閽堝IE鎴栬�呬互IE涓哄唴鏍哥殑娴忚鍣細 + if(userAgent.contains("MSIE")||userAgent.contains("Trident")) { + fileName = URLUtil.encode(fileName, Charset.forName("UTF-8")); + } else { + // 闈濱E娴忚鍣ㄧ殑澶勭悊锛� + fileName = new String(fileName.getBytes("UTF-8"),"ISO-8859-1"); + } + res.setHeader("Content-disposition", "attachment; filename=" + fileName); + res.setContentType("application/vnd.ms-excel; charset=utf-8"); + res.setCharacterEncoding("UTF-8"); + // 灏嗘祦涓唴瀹瑰啓鍑哄幓 + try (OutputStream outputStream = res.getOutputStream()) { + workbook.write(outputStream); + outputStream.flush(); + } catch (IOException e) { + e.printStackTrace(); + throw new RuntimeException("妯℃澘涓嬭浇寮傚父"); + } + } } -- Gitblit v1.9.1