JAVA 导出 Excel, 将Excel下载到本地
昨天说了如何将数据导出成 excel 的两种方式,今天完善一下将 java 导出(文件在服务器)并下载到本地
1. 触发导出 ajax 代码
$.ajax({
type: "POST",
url: "${ctx}/website/clsecurity/XXXXXAction_exportUserinfoData.do",
async: false,
dataType: "json",
data: {
"province": province,
"userType": userType,
"startDate": startDate,
"endDate": endDate
},
success: function(data) {
var json = eval( '(' + data + ')' );
window.open("${ctx}" + json.url);
}
});
2. 处理导出的方法代码片段
List<ClUserinfo> regUsers = clSecurityService.findClUserinfos(clSecurityForm);
List<TempUser> userList = new ArrayList<TempUser>();
for (ClUserinfo userinfo : regUsers) {
TempUser user = new TempUser();
user.setUserName(userinfo.getUserName());
user.setUserType(userinfo.getUserType());
user.setCompany(userinfo.getCompany());
user.setKeshi(userinfo.getKeshi());
user.setProvince(userinfo.getProvince());
user.setCreatedDate(userinfo.getCreatedDate());
user.setCreateDateStr(userinfo.getCreatedDate().toString());
userList.add(user);
}
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("注册用户表");
HSSFRow row = sheet.createRow((int) 0);
HSSFCellStyle style = wb.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
HSSFCell cell = row.createCell((short) 0);
cell.setCellValue("姓名");
cell.setCellStyle(style);
cell = row.createCell((short) 1);
cell.setCellValue("专委会");
cell.setCellStyle(style);
cell = row.createCell((short) 2);
cell.setCellValue("单位");
cell.setCellStyle(style);
cell = row.createCell((short) 3);
cell.setCellValue("科室");
cell.setCellStyle(style);
cell = row.createCell((short) 4);
cell.setCellValue("注册日期");
cell.setCellStyle(style);
for (int i = 0; i < userList.size(); i++) {
row = sheet.createRow((int) i + 1);
TempUser user = (TempUser) userList.get(i);
row.createCell((short) 0).setCellValue(user.getUserName());
row.createCell((short) 1).setCellValue(user.getUserType());
row.createCell((short) 2).setCellValue(user.getCompany());
row.createCell((short) 3).setCellValue(user.getKeshi());
row.createCell((short) 4).setCellValue(user.getCreateDateStr());
}
try {
String fileName = "RegistUserList-" + new SimpleDateFormat("yyyyMMddhhmmss").format(new Date()) + ".xls";
String savaPath = ServletActionContext.getRequest().getRealPath("/upload/excel");
FileOutputStream fos = new FileOutputStream(savaPath + "\\" + fileName);
wb.write(fos);
fos.close();
Map<String,String> map = new HashMap<String,String>();
map.put("url", "/upload/excel/" + fileName);
JSONObject json = JSONObject.fromObject(map);
this.result = json.toString();
} catch (Exception e) {
e.printStackTrace();
}
3. result 装返回结果
/**
* json return
*/
private String result; public String getResult() {
return result;
}
public void setResult(String result) {
this.result = result;
}
4. XXXXXAction_XXXXXXX 方法以及 result 在 struts2 的配置
<action name="XXXXXAction_exportUserinfoData" class="com.gzewell.ucomweb.web.security.action.XXXXXAction" method="exportUserinfoData">
<result name="success" type="json">
<param name="root">result</param>
</result>
</action>
说明:
fos:文件输出流,将文件放在服务器的 /upload/excel 目录
result: 将路径放入map 结果以 json 的形式返回
window.open:打开新的页面,即为需要下载的文件在服务器的位置
${ctx}: 为服务器地址ip | 网址的表达式 (127.0.0.1)
JAVA 导出 Excel, 将Excel下载到本地的更多相关文章
- 通过Java HTTP连接将网络图片下载到本地
通过Java HTTP连接将网络图片下载到本地 只知道浏览器使用的是HTTP协议,那么如何将网络资源使用JavaHTTP下载下来呢! 这只是一个非常简单的小示例,只是不想每次碰到关于此方面的内容忘 ...
- Java 导出大批量数据excel(百万级)(转载)
参考资料:http://bbs.51cto.com/thread-1074293-1-1.html http://bbs.51cto.com/viewthread.ph ...
- POI Java 导出数据到Excel
<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifac ...
- JAVA导出数据到excel中大数据量的解决方法
最近在做项目功能时 ,发现有20万以上的数据.要求导出时直接导出成压缩包.原来的逻辑是使用poi导出到excel,他是操作对象集合然后将结果写到excel中. 使用poi等导出时,没有考虑数据量的问题 ...
- java导出和读取excel数据
使用的是poi的jar包 下载地址http://poi.apache.org/download.html 主要是把jar包导入,直接新建一个列子测试一下就明白了.使用起来还是比较方便的,代码里面的原理 ...
- java导出数据到excel里:直接导出和导出数据库数据
一.直接导出 package com.ij34.util; import java.io.FileNotFoundException; import java.io.FileOutputStream; ...
- Java导出数据生成Excel表格
事先准备: 工具类: package com.wazn.learn.util.export; import java.sql.Connection; import java.sql.DriverMan ...
- 【转】java导出多个excel表格,并压缩成zip输出
转自:http://blog.csdn.net/qq_14861089/article/details/53169414 感谢作者分享 /** * 导出支付宝批量支付文件excel * * @p ...
- Java导出数据为EXCEL的两种方式JXL和POI
JXL和POI导出数据方式的比较 POI支持excel2003和2007,而jxl只支持excel2003. 下面为测试代码: public class TestCondition { /** * 生 ...
- python 生成excel,并下载到本地
from django.shortcuts import reverse,redirect,render from operations import models import xlwt impor ...
随机推荐
- ASP.NET MVC ScriptBundle 不能加载.min.js文件
比如我用 bundles.Add(new ScriptBundle("~/bundles/easyui").Include( "~/Content/easyui/jque ...
- ASP.NET Web API 2.0 统一响应格式
传统实现 在搭建 Web API 服务的时候,针对客户端请求,我们一般都会自定义响应的 JSON 格式,比如: { "Data" : { "Id" : 100, ...
- Visual Studio Code 学习.net core初体验
一,安装 最近在用 Visual Studio Code 学习.net core ,记录下学习的过程,首先去官网下载最新的.net core2.1安装包,有windows 和mac,根据自己的开发环境 ...
- .Net Core2.0中使用ADO.NET
学习了解.NET CORE有段时间,没有用其做项目的主要原因就是这么多年积累的类库兼容问题.今天就先解决SqlHelper的兼容性: 建立类库,目标框架选择.NET Core2.0,复制粘贴代码. 问 ...
- Newtonsoft.Json日常用法
原文链接:https://www.cnblogs.com/ZengJiaLin/p/9578794.html
- Linux中目录proc/net/dev详解
在Linux系统中,系统调用是操作系统提供给应用程序使用操作系统服务的重要接口,但同时也正是通过系统调用机制,操作系统屏蔽了用户直接访问系统内核的可能性.幸运的是Linux提供了LKM机制可以使我们在 ...
- Visual Studio在Win10中以管理员方式运行
在Win10中运行VS不是默认以管理员程序运行的,需要手动设置 第一步:将VS快捷方式设置为以管理员身份运行 第二部 在C:\Program Files\Microsoft Visual Studio ...
- pageadmin CMS网站制作教程:模板中的站点数据调用
pageadmin CMS网站建设教程:模板中的站点数据调用 1.获取当前站点Id,返回int数字 Html.CurrentSiteId() 2.获取当前站点url地址,返回string字符串 Htm ...
- AndroidStudio的一些快捷键的使用
1.返回上一次浏览快捷键的设置 https://blog.csdn.net/yingtian648/article/details/73277388 2.格式化代码的快捷键的设置 htt ...
- 670. Maximum Swap
Given a non-negative integer, you could swap two digits at most once to get the maximum valued numbe ...