js代码
/* 进度条隐藏 */
function modalClose() {
//$("#myModal").modal('hide');
$('#PerExcelModal').modal('hide');
$(".modal-backdrop").remove();
$("body").removeClass('modal-open');
//$("#d").trigger('click');
}
function ajax_send(func){
$.ajax({
type:"POST",
url: "${base}/business/order/isExport",
datatype:"json",
success:function(res){
var res=JSON.parse(res);
func.call(this,res);
}
});
}
function aa(){
var interval = setInterval(function(){
ajax_send(function(res){
if(res){
modalClose();
clearInterval(interval);
}else{
};
});
},1000); //每隔1秒调用一次函数
};
//导出excel
$repExcel.click(function(){
var status=$("#status").val();
$("#prestatus").val(status);
var type=$("#type").val();
$("#pretype").val(type);
/* var searchValue=$("#searchValue").val();
$("#presearchValue").val(searchValue); */
$("#repFrom").submit();
//setTimeout(modalClose, 5000);
aa();
});
controller 代码:
/**
* 订单导出Excel
* @throws IOException
*/
@RequestMapping("/reportExcelOrders")
public ResponseEntity> reportExcelOrders(HttpServletRequest request,HttpServletResponse response,Order.Type type, Order.Status status, String memberUsername, Boolean isPendingReceive, Boolean isPendingRefunds, Boolean isAllocatedStock, Boolean hasExpired, Pageable pageable, @CurrentStore Store currentStore, ModelMap model,@CurrentUser Business business) throws IOException{
//条件查询数据
Member member = memberService.findByUsername(memberUsername);
pageable.setPageSize(Integer.MAX_VALUE);
List
// List
// List
// if(alls.size()<100){
// for (int i = 0; i
// }
// }else {
// for (int i = 0; i <99; i++) {
// all.add(alls.get(i));
// }
// }
// if(all==null){
// return Results.badRequest("数据为空,不能导出");
// }
// 查询出 满足当前条件 结果数据
//List
// List
HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
HSSFSheet sheet = hssfWorkbook.createSheet("订单数据");
HSSFRow headRow = sheet.createRow(0);
headRow.createCell(0).setCellValue("订单号");
headRow.createCell(1).setCellValue("产品条码");
headRow.createCell(2).setCellValue("订单状态");
headRow.createCell(3).setCellValue("买家id");
headRow.createCell(4).setCellValue("子订单编号");
headRow.createCell(5).setCellValue("买家昵称");
headRow.createCell(6).setCellValue("商品名称");
headRow.createCell(7).setCellValue("产品规格");
headRow.createCell(8).setCellValue("商品单价");
headRow.createCell(9).setCellValue("商品数量");
headRow.createCell(10).setCellValue("商品总价");
headRow.createCell(11).setCellValue("运费");
headRow.createCell(12).setCellValue("购买优惠信息");
headRow.createCell(13).setCellValue("总金额");
headRow.createCell(14).setCellValue("买家购买附言");
headRow.createCell(15).setCellValue("收货人姓名");
headRow.createCell(16).setCellValue("收货地址-省市");
headRow.createCell(17).setCellValue("收货地址-街道地址");
headRow.createCell(18).setCellValue("邮编");
headRow.createCell(19).setCellValue("收货人手机");
headRow.createCell(20).setCellValue("收货人电话");
headRow.createCell(21).setCellValue("买家选择运送方式");
headRow.createCell(22).setCellValue("卖家备忘内容");
headRow.createCell(23).setCellValue("订单创建时间");
headRow.createCell(24).setCellValue("付款时间");
headRow.createCell(25).setCellValue("物流公司");
headRow.createCell(26).setCellValue("物流单号");
headRow.createCell(27).setCellValue("发货附言");
headRow.createCell(28).setCellValue("发票抬头");
headRow.createCell(29).setCellValue("电子邮件");
// 表格数据
for (Order order : all) {
HSSFRow dataRow = sheet.createRow(sheet.getLastRowNum() + 1);
dataRow.createCell(0).setCellValue(order.getSn());
dataRow.createCell(1).setCellValue(order.getSn());
if(Order.Status.PENDING_PAYMENT.equals(order.getStatus())){
dataRow.createCell(2).setCellValue("等待付款");
}else if(Order.Status.PENDING_REVIEW.equals(order.getStatus())){
dataRow.createCell(2).setCellValue("等待审核");
}else if(Order.Status.PENDING_SHIPMENT.equals(order.getStatus())){
dataRow.createCell(2).setCellValue("等待发货");
}else if(Order.Status.SHIPPED.equals(order.getStatus())){
dataRow.createCell(2).setCellValue("已发货");
}else if(Order.Status.RECEIVED.equals(order.getStatus())){
dataRow.createCell(2).setCellValue("已收货");
}else if(Order.Status.COMPLETED.equals(order.getStatus())){
dataRow.createCell(2).setCellValue("已完成");
}else if(Order.Status.FAILED.equals(order.getStatus())){
dataRow.createCell(2).setCellValue("已失败");
}else if(Order.Status.CANCELED.equals(order.getStatus())){
dataRow.createCell(2).setCellValue("已取消");
}else if(Order.Status.DENIED.equals(order.getStatus())){
dataRow.createCell(2).setCellValue("已拒绝");
}else if(Order.Status.DELETE.equals(order.getStatus())){
dataRow.createCell(2).setCellValue("已删除");
}
dataRow.createCell(3).setCellValue(order.getMember().getId());
dataRow.createCell(4).setCellValue("");//子订单编号
dataRow.createCell(5).setCellValue(order.getMember().getUsername());
List
for (OrderItem orderItem : orderItems) {
dataRow.createCell(6).setCellValue(orderItem.getName());
dataRow.createCell(7).setCellValue(orderItem.getSpecifications().toString());
dataRow.createCell(8).setCellValue(orderItem.getPrice().toString());
}
dataRow.createCell(9).setCellValue(order.getQuantity());
dataRow.createCell(10).setCellValue(order.getAmount().toString());
dataRow.createCell(11).setCellValue(order.getFreight().toString());
dataRow.createCell(12).setCellValue("");
dataRow.createCell(13).setCellValue(order.getAmount().toString());
dataRow.createCell(14).setCellValue(order.getMemo());
dataRow.createCell(15).setCellValue(order.getConsignee());
dataRow.createCell(16).setCellValue(order.getAreaName());
dataRow.createCell(17).setCellValue(order.getAddress());
dataRow.createCell(18).setCellValue(order.getZipCode());
//收货人手机
dataRow.createCell(19).setCellValue(order.getPhone());
//收货人电话
dataRow.createCell(20).setCellValue(order.getPhone());
dataRow.createCell(21).setCellValue(order.getShippingMethodName());
dataRow.createCell(22).setCellValue("");
dataRow.createCell(23).setCellValue(order.getCreatedDate());
//付款时间
OrderPayment orderPayment = orderPaymentService.findBySn(order.getSn());
if(orderPayment!=null){
dataRow.createCell(24).setCellValue(orderPayment.getCreatedDate());
}
//物流
OrderShipping orderShipping=orderShippingService.findOrder(order);
if(orderShipping!=null){
dataRow.createCell(25).setCellValue(orderShipping.getDeliveryCorp());
dataRow.createCell(26).setCellValue(orderShipping.getTrackingNo());
dataRow.createCell(27).setCellValue(orderShipping.getMemo());
}
dataRow.createCell(28).setCellValue(order.getInvoice()!=null?order.getInvoice().getTitle():"");//发票抬头
dataRow.createCell(29).setCellValue(order.getMember().getEmail());//电子邮件
}
// 下载导出
// 设置头信息
// ServletActionContext.getResponse().setContentType(
// "application/vnd.ms-excel");
response.setContentType("application/vnd.ms-excel");
String filename = "订单数据.xls";
String agent = request.getHeader("user-agent");
filename = FileUtils.encodeDownloadFilename(filename, agent);
response.setHeader("Content-Disposition",
"attachment;filename=" + filename);
ServletOutputStream outputStream = response.getOutputStream();
hssfWorkbook.write(outputStream);
// 关闭
hssfWorkbook.close();
//导出完成,更改状态
statusEXcel=true;
return Results.OK;
}
//判断是否导出完毕的方法
/**
* @Description: 是否导出完毕
* @version: v1.0.0
* @date: 2018年9月18日 下午4:32:32
*
* Modification History:
* Date Author Version Description
*-------------------------------------------------------------
*/
@PostMapping("/isExport")
@ResponseBody
public Boolean isExport(HttpServletRequest request,HttpServletResponse response){
// Object exportedFlag = request.getSession().getAttribute("exportedFlag");
if(statusEXcel){
//已经导完;
statusEXcel=false;
return true;
}else{
//还未导完;
return false;
}
}
工具类:
import java.io.IOException;
import java.net.URLEncoder;
import sun.misc.BASE64Encoder;
public class FileUtils {
public static String encodeDownloadFilename(String filename, String agent)
throws IOException {
if (agent.contains("Firefox")) { // 火狐浏览器
filename = "=?UTF-8?B?"
+ new BASE64Encoder().encode(filename.getBytes("utf-8"))
+ "?=";
filename = filename.replaceAll("\r\n", "");
} else { // IE及其他浏览器
filename = URLEncoder.encode(filename, "utf-8");
filename = filename.replace("+"," ");
}
return filename;
}
}