环境
后台语言: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
的方式,就可以。当然表单提交也可以。
记录下。前端不是很懂。