ajax做excel导出

环境

后台语言: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的方式,就可以。当然表单提交也可以。

记录下。前端不是很懂。

相关文章

JS原生Ajax操作(XMLHttpRequest) GET请求 POST请求 兼容性问题 利用iframe模拟ajax 实现表单提交的返回...
AJAX 每日更新前端基础,如果觉得不错,点个star吧 😃 https://github.com/WindrunnerMax/E...
踩坑Axios提交form表单几种格式 前后端分离的开发前后端, 前端使用的vue,后端的安全模块使用的SpringSe...
很早就听闻ajax的名声,但是却一直不知道怎么用,今天自己捣鼓了一下,竟然会用了,哈哈哈哈。 为了防止...
需要在服务器上进行哈 jquery的ajax方法: // jquery请求 $.ajax({ url: "./server/slider.js...
Ajax函数封装ajax.js // Get / Post // 参数 get post // 是否异步 // 如何处理响应数据 // URL // var...