function getExcel() {
location.href=prefix + "/export"+"?userId="+$('#userId').val()+"&userName="+$('#userName').val();
// $.ajax({
// type : "post",
// url : prefix + "/export",
// async : false,
// dataType : "json",
// data : {
"status" : $('#status').val(),
// "userId" :$('#userId').val(),
// "userName" : $('#userName').val(),
// "createTime" :$('#createTime').val()
// },
// success : function(data) {
// var json = eval('(' + data + ')');
// window.open("${ctx}" +json.url);
// }
// });
}
<!--这里不能用ajax请求,ajax请求无法弹出下载保存对话框-->
- Query query = new Query();
if (userName.length() > 0) {
query.addCriteria(Criteria.where("userName").regex("^.*?" + userName + ".*"));
}
if (userId != null) {
query.addCriteria(Criteria.where("userId").is(userId));
}
if (createTime != null) {
query.addCriteria(Criteria.where("createTime").gte(createTime));
}
List<BtcDetail> btcDetails = mongoTemplate.find(query, BtcDetail.class);
String[] title = {"钱包明细流水号","userId","单位","提币数量、充币数量","status"};
String fileName="Feng.xls";
String sheetName = "BtcDetail表";
String content[][]=new String[btcDetails.size()][title.length];
for (int i = 0; i < btcDetails.size(); i++) {
content[i] = new String[title.length];
BtcDetail btc = btcDetails.get(i);
content[i][0] = btc.getDetailId();
content[i][1] = btc.getUserId().toString();
content[i][2] = btc.getUnit();
content[i][3] = btc.getAmount();
Integer status = btc.getStatus();
if (status == 1) content[i][4] ="转入";
if (status == 2) content[i][4] ="提币中";
if (status == 3) content[i][4] ="提币完成";
if (status == 4) content[i][4] ="提币失败";
}
HSSFWorkbook wb = ExcelUtil.getHSSFWorkbook(sheetName, title, content, null);
try {
new ExcelUtil().setResponseHeader(response, fileName);
OutputStream os = response.getOutputStream();
wb.write(os);os.flush();os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
-
import java.io.UnsupportedEncodingException;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;public class ExcelUtil {
/**
* 导出Excel
* @param sheetName sheet名称
* @param title 标题
* @param values 内容
* @param wb HSSFWorkbook对象
* @return
*/
public static HSSFWorkbook getHSSFWorkbook(String sheetName,String []title,String [][]values, HSSFWorkbook wb){// 第一步,创建一个HSSFWorkbook,对应一个Excel文件
if(wb == null){
wb = new HSSFWorkbook();
}// 第二步,在workbook中添加一个sheet,对应Excel文件中的sheet
HSSFSheet sheet = wb.createSheet(sheetName);// 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制
HSSFRow row = sheet.createRow(0);// 第四步,创建单元格,并设置值表头 设置表头居中
HSSFCellStyle style = wb.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式//声明列对象
HSSFCell cell = null;//创建标题
for(int i=0;i<title.length;i++){
cell = row.createCell(i);
cell.setCellValue(title[i]);
cell.setCellStyle(style);
}//创建内容
for(int i=0;i<values.length;i++){
row = sheet.createRow(i + 1);
for(int j=0;j<values[i].length;j++){
//将内容按顺序赋给对应的列对象
row.createCell(j).setCellValue(values[i][j]);
}
}
return wb;
}
//发送响应流方法
public void setResponseHeader(HttpServletResponse response, String fileName) {
try {
try {
fileName = new String(fileName.getBytes(),"ISO8859-1");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
response.setContentType("application/octet-stream;charset=ISO8859-1");
response.setHeader("Content-Disposition", "attachment;filename="+ fileName);
response.addHeader("Pargam", "no-cache");
response.addHeader("Cache-Control", "no-cache");
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
ExcelUtil 工具类也不知道在哪看到的了,因为弄这个搜了一大堆代码 原作者看到请见谅