ajax做excel导出

7 篇文章 0 订阅
3 篇文章 1 订阅

环境

后台语言:java
框架:play framework1

ajax好像不能直接用过做excel导出

今天测试了下:
前端代码:

    $("#export").click(function(){
        var startTime = $("#startTime").val();
        $.ajax({
            url: "@{DownClickGogoalAction.export()}",
            type: "POST",
            data: {startTime:$("#startTime").val(), endTime:$("#endTime").val()},
            dataType: "JSON",
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                alert("网络错误");
            },
            success: function(data) {
                }
            })
    });

后台代码:

public static void export(Date startTime, Date endTime){
    DBCollection gogoalSum = GGMongoOperator.getGGBusinessCollection("gg_gogoal_statistic_sum");
BasicDBObject timeQuery = new BasicDBObject();
if(startTime != null){
    timeQuery.append("$gte", startTime);
}
if(endTime != null){
    timeQuery.append("$lte", endTime);
}

XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet("安装&下载&卸载");
XSSFRow row = sheet.createRow(0);
XSSFCell cell = row.createCell(0);
cell.setCellValue("日期");
cell = row.createCell(1);
cell.setCellValue("下载量");
cell = row.createCell(2);
cell.setCellValue("安装成功量");
cell = row.createCell(3);
cell.setCellValue("安装失败量");
cell = row.createCell(4);
cell.setCellValue("卸载量");
BasicDBObject query = new BasicDBObject();
if(!timeQuery.isEmpty()){
    query.append("downdate", timeQuery);
}

DBCursor cursor = gogoalSum.find(query).sort(new BasicDBObject("downdate", -1)).limit(10);
int rowCount = 1;
while(cursor.hasNext()){
    DBObject o = cursor.next();
    Object date = o.get("downdate");
    if(date != null){
        int down_click_sum = (Integer) (o.get("down_click_sum")==null?0:o.get("down_click_sum"));
        int install_sum = (Integer) (o.get("install_sum")==null?0:o.get("install_sum"));
        int installFail_sum = (Integer) (o.get("installFail_sum")==null?0:o.get("installFail_sum"));
        int uninstall_sum = (Integer) (o.get("uninstall_sum")==null?0:o.get("uninstall_sum"));

        row = sheet.createRow(rowCount);
        cell = row.createCell(0);
        cell.setCellValue((Date)date);
        CellStyle cellStyle = workbook.createCellStyle();//创建单元格样式
        DataFormat format= workbook.createDataFormat();//设置时间类型格式
        cellStyle.setDataFormat(format.getFormat("yyyy-MM-dd"));//设置时间类型格式
        sheet.autoSizeColumn(0);//使单元格的第0列,宽度自适应,否则宽度不够会显示#号
        cell.setCellStyle(cellStyle);//设置时间类型格式

        cell = row.createCell(1);
        cell.setCellValue(down_click_sum);
        cell = row.createCell(2);
        cell.setCellValue(install_sum);
        cell = row.createCell(3);
        cell.setCellValue(installFail_sum);
        cell = row.createCell(4);
        cell.setCellValue(uninstall_sum);
        rowCount++;
    }
}
cursor.close();

long currentTimeMillis = System.currentTimeMillis();

String fileName = "下载&安装&卸载" + currentTimeMillis + ".xlsx";

try {
    String fileNa  = java.net.URLEncoder.encode(fileName != null ? fileName : "数据导出.xls", "UTF-8");//先把中文使用utf-8进行编码
    response.setContentTypeIfNotSet("application/vnd.ms-excel;charset=UTF-8");//告诉浏览器数据类型
    response.setHeader("Content-disposition", "attachment;filename=" + fileNa); //激活文件下载对话框
        workbook.write(response.out);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

代码是没有问题,但是这样总是报网络错误,网上的解释是,ajax不支持excel格式数据的。

假设我把前端代码改为:

$("#export").click(function(){
   #{if result[0]}
     window.location.href="@{DownClickGogoalAction.export()}?startTime="+ $("#startTime").val()+"&endTime="+$("#endTime").val();
    #{/if}
    #{else}
          layer.alert("暂无数据可导!");
    #{/else}
});

也就是使用window.location.href的方式,就可以。当然表单提交也可以。

记录下。前端不是很懂。

这里写图片描述

评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

山鬼谣me

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值