关键步骤在于
1.传入response
2.设置response的头部文件类型和 把工作簿写入 response的 outputStream中即可。无需特意返回response。
response.setContentType("application/vnd.ms-excel;charset=utf-8");
response.setHeader("Content-Disposition", "attachment;filename="+ new String((fileName + ".xls").getBytes(), "iso-8859-1"));
//根据传进来的file对象创建可写入的Excel工作薄
OutputStream os = response.getOutputStream();
WritableWorkbook wwb = null;
try {
首先要使用Workbook类的工厂方法创建一个可写入的工作薄(Workbook)对象
wwb = Workbook.createWorkbook(os);
ps: jsp中请求的action 记得要return null,因为 已经包含了 文件流的response。
如果不是return null的话则会冲突报错。
优化
jsp页面 使用js 提交 post form
$(document).ready(function() {
var $searchForm = $('#search_form').on('submit',function(){
$dt.DataTable().searchEx( {} ).draw();
return false;
}).on('click', 'button.export', function(){
var searchData={};
searchData.search=$('#search_form').formGet();
console.log(searchData);
post('/order/list/export',searchData);
});
function post(URL, PARAMS) {
var temp = document.createElement("form");
temp.action = URL;
temp.method = "post";
temp.style.display = "none";
for (var x in PARAMS.search) {
var opt = document.createElement("textarea");
if(x=="id"||x=="expressNumber"||x=="payStatus"){
opt.name = x;
opt.value = PARAMS.search[x];
temp.appendChild(opt);
}
}
document.body.appendChild(temp);
temp.submit();
}
}
controller.class接收
@RequestMapping("/product-upgrade/list/export")
public void export(HttpServletRequest request, HttpServletResponse response) {
Map<String, String> search = new HashMap<>();
Enumeration<String> parameterNames = request.getParameterNames();
while (parameterNames.hasMoreElements()) {
String key = parameterNames.nextElement();
search.put(key, request.getParameter(key));
}
Query query = new Query();
Criteria criteria = new Criteria();
criteria.and("upgradeList").exists(true);
if (!StringUtils.isBlank(search.get("mobile")))
criteria.and("mobile").is(search.get("mobile"));
if (!StringUtils.isBlank(search.get("payStatus")))
criteria.and("payStatus").is(Integer.parseInt(search.get("payStatus")));
if (!StringUtils.isBlank(search.get("upgradeStatus"))) {
if("0".equals(search.get("upgradeStatus"))) {
criteria.orOperator(Criteria.where("upgradeStatus").exists(false), Criteria.where("upgradeStatus").is(0));
}else {
criteria.and("upgradeStatus").is(Integer.parseInt(search.get("upgradeStatus")));
}
}
if (!StringUtils.isEmpty(search.get("beginTime"))
|| !StringUtils.isEmpty(search.get("endTime"))) {
criteria = criteria.and("createDate");
if (!StringUtils.isEmpty(search.get("beginTime")))
criteria.gte(DateUtils.parse(search.get("beginTime")));
if (!StringUtils.isEmpty(search.get("endTime")))
criteria.lte(DateUtils.parse(search.get("endTime")));
}
query.addCriteria(criteria);
query.with(new Sort(Sort.Direction.DESC, "createDate"));
List<Order> list = mongoTemplate.find(query, Order.class);
exportProductUpgradeOrder(list, response);
}
@Override
public boolean exportProductUpgradeOrder(List<Order> orders, HttpServletResponse response) {
try {
ArrayList<String[]> contentsArrayList = new ArrayList<>();
String[] titles = new String[]{
"订单编号", "订单名称", "唾液盒编号", "手机", "总金额", "在线支付",
"支付状态", "升级状态", "创建时间", "升级备注"};
Boolean[] titleNumFlags = new Boolean[]{
false, false, false, false, true, true,
false, false, false, false};// 是否是数值型的标识
contentsArrayList.add(titles);
for (Order order : orders) {
String[] content = new String[titles.length];
content[0] = order.getId();
content[1] = order.getOrderName();
content[2] = order.getUpgradeList().get(0).getBarcode();
content[3] = order.getMobile();
content[4] = String.valueOf(order.getTotalMoney());
content[5] = String.valueOf(order.getPayMoney());
content[6] = order.getPayStatus() == Order.PAY_UNPAY ? "未付款" : "已付款";
content[7] = order.getUpgradeStatus() == 1 ? "已处理" : "未处理";
content[8] = order.getCreateDate();
content[9] = order.getUpgradeRemark();
contentsArrayList.add(content);
}
ExcelUtils.writeExcel(ExcelUtils.changeToArray(contentsArrayList), "导出", response, titleNumFlags);
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
} |