文章目录 浏览器下载json普通文件 浏览器下载excel文件【借助Workbook】
浏览器下载json普通文件
@ProjectResource @Operation ( summary = "设备模型导出(带分组)" ) @PostMapping ( "/export" ) @AuditLog ( source = AuditSourceType . PLATFORM , operationEvent = "设备模型导出(带分组)" , resourceType = "${knife4j.openapi.description}" , operType = AuditOperationType . EXPORT ) public void exportWithGroup ( HttpServletRequest request, HttpServletResponse response) { var user = SecurityUtils . getCurrentUser ( ) ; bizTypeService. exportWithGroup ( request, response, user) ; } @Override public void exportWithGroup ( HttpServletRequest request, HttpServletResponse response, SecurityUser user) { ExportDto exportDto = new ExportDto ( ) ; exportDto. setTypes ( types) ; exportDto. setGroups ( groups) ; PrintWriter writer = null ; try { response. setContentType ( "application/octet-stream" ) ; response. setCharacterEncoding ( "utf-8" ) ; response. setHeader ( "content-disposition" , "download;filename*=utf-8''" + URLEncoder . encode ( "device_model_type.json" , "UTF-8" ) ) ; writer = response. getWriter ( ) ; writer. write ( JacksonUtils . writeValueAsString ( exportDto) ) ; } catch ( Exception e) { throw new RuntimeException ( e) ; } finally { writer. close ( ) ; } }
浏览器下载excel文件【借助Workbook】
@Override public void exportRunningRecords ( HttpServletRequest request, HttpServletResponse response) { List < RunningRecordsDto > data = resultDto. getItems ( ) ; List < String > headers = new ArrayList < > ( ) ; headers. add ( "设备名称" ) ; headers. add ( "设备编号" ) ; headers. add ( "所属模型" ) ; headers. add ( "安装位置" ) ; headers. add ( "服务范围" ) ; headers. add ( "运行次数" ) ; headers. add ( "运行时长" ) ; List < List < Object > > dataList = new ArrayList < > ( ) ; for ( RunningRecordsDto item : data) { List < Object > list = new ArrayList < > ( ) ; list. add ( item. getName ( ) ) ; list. add ( item. getCode ( ) ) ; list. add ( item. getTypeName ( ) ) ; list. add ( item. getLocationName ( ) ) ; list. add ( item. getServiceArea ( ) ) ; list. add ( item. getCounts ( ) ) ; list. add ( item. getDuration ( ) ) ; dataList. add ( list) ; } try { ExcelUtils . writeExcel ( headers, dataList, "设备运行记录.xlsx" , request, response) ; } catch ( Exception e) { log. error ( "设备运行记录导出异常" , e) ; } } public static void writeExcel ( List < String > headers, List < List < Object > > datas, String fileName, HttpServletRequest request, HttpServletResponse response) throws Exception { Workbook workbook = generateWorkBook ( headers, datas) ; final String userAgent = request. getHeader ( "USER-AGENT" ) ; if ( StringUtils . contains ( userAgent, MSIE ) || StringUtils . contains ( userAgent, "Trident" ) || StringUtils . contains ( userAgent, "Edge" ) ) { fileName = new String ( URLEncoder . encode ( fileName, "UTF-8" ) . getBytes ( "UTF-8" ) , "ISO-8859-1" ) ; } else if ( StringUtils . contains ( userAgent, MOZILLA ) ) { fileName = URLEncoder . encode ( fileName, "UTF-8" ) ; } response. reset ( ) ; response. setContentType ( XLSX_CONTENT_TYPE ) ; response. setHeader ( "content-disposition" , "attachment;filename=" + fileName) ; response. setCharacterEncoding ( "UTF-8" ) ; try { workbook. write ( response. getOutputStream ( ) ) ; } catch ( IOException e) { throw new IOException ( "could not write to response. cause: " , e) ; } finally { if ( workbook != null ) { workbook. close ( ) ; } } }