我是靠谱客的博主 爱笑镜子,这篇文章主要介绍java下载excel,现在分享给大家,希望可以做个参考。

下载文件相关:使用阿里的excelExcel,下面是相关下载代码

java端代码: 

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/** * 下载excel * * @param response * @param fileName  * @param data 数据可谓null  * @param t 实体类根据注解生成表头 */ public static <T extends BaseRowModel> void downLoadExcel(HttpServletResponse response, String fileName, List<T> data, Class<T> t) { ServletOutputStream out = null; try { response.setContentType("application/vnd.ms-excel;charset=UTF-8"); response.setCharacterEncoding("utf-8"); fileName = new String((fileName + new SimpleDateFormat("yyyy-MM-dd").format(new Date())).getBytes("UTF-8"), "iso-8859-1"); response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx"); out = response.getOutputStream(); ExcelWriter writer = new ExcelWriter(out, ExcelTypeEnum.XLSX); Sheet sheet1 = new Sheet(1, 0, t); sheet1.setSheetName("sheet1"); writer.write(data, sheet1); writer.finish(); out.flush(); } catch (Exception e) { e.printStackTrace(); } finally { if (out != null) { try { out.close(); } catch (IOException e) { e.printStackTrace(); } } } }

 

前段代码,利用jquery无刷新页面进行下载文件

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// Ajax 文件下载 //当不用传参时,可以将data去掉 function download (url, data, method){ // 获得url和data if( url){ // data 是 string 或者 array/object if(data != null && data != ''){ data = typeof data == 'string' ? data : jQuery.param(data); // 把参数组装成 form的 input var inputs = ''; jQuery.each(data.split('&'), function(){ var pair = this.split('='); inputs+='<input type="hidden" name="'+ pair[0] +'" value="'+ pair[1] +'" />'; }); // request发送请求 } jQuery('<form action="'+ url +'" method="'+ (method||'post') +'">'+inputs+'</form>').appendTo('body').submit().remove(); }; };

最后

以上就是爱笑镜子最近收集整理的关于java下载excel的全部内容,更多相关java下载excel内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(52)

评论列表共有 0 条评论

立即
投稿
返回
顶部