//*********************************************************** 说明 **********************************************************
注释掉的都是之前走的弯路,可以导出,但是浏览器没有下载提示,没反应,用户体验不好,主要是换了个输出流,浏览器现在有个下载成功的提示。
//*********************************************************** js代码 **********************************************************
function exportExcel(){
var codes = [];
var areaId = $("#area").val();
var cityId = $("#city").val();
var provinceId = $("#province").val();
$.each($('input:checkbox:checked'),function(){
codes.push($(this).val());
});
var title = "序号,用户,身份证号,电话,行政区,职位,行政区级别,留言,审核状态";
// location.href="../../../ApplyEntrepreneurialInfo_Controller_4W/exportExcel.action";
location.href="../../../ApplyEntrepreneurialInfo_Controller_4W/provinceExportExcel.action?provinceId="+provinceId+"&cityId="+cityId+"&areaId="+areaId+"&codes="+codes+"&title="+title+"&job_levels=3&agentName=区县级代理商审批通过信息"
// $.ajax({
// type: "get",
// url: "../../../ApplyEntrepreneurialInfo_Controller_4W/provinceExportExcel.action",
// data: {"provinceId":provinceId,"cityId":cityId,"areaId":areaId, "codes":codes,"title":title,"job_levels":3,"agentName":"区县级代理商审批通过信息"},
// dataType: "json",
// success: function(data){
// alert("下载成功");
// }
// });
}
//*************************************************************************************************************************************
/**
* 后台代码--后台代码--后台代码--后台代码--后台代码--后台代码--后台代码--
* @param response 必须要的
* @param applyEntrepreneurialInfo 我查询sql需要的参数
* @param title 列名
* @param agentName 用于文件名和sheet名
*/
//导出各代理商审批通过信息
@RequestMapping(value = "provinceExportExcel",method = RequestMethod.GET)
@ResponseBody
public void provinceExportExcel(HttpServletResponse response,ApplyEntrepreneurialInfo applyEntrepreneurialInfo,String title,String agentName) {
try {
//准备数据
List<T_dictionary> t_dictionoaries=null;
List<ApplyEntrepreneurialInfo> list = this.applyEntrepreneurialInfoService.selectApplyEntrepreneurialInfoAccess(applyEntrepreneurialInfo);
for(int i =0 ;i<list.size();i++){
String administrative_area_levels =list.get(i).getAdministrative_area_levels();
if(administrative_area_levels.equals("1")){
list.get(i).setAdministrative_area_levels("省级代理");
}else if(administrative_area_levels.equals("2")){
list.get(i).setAdministrative_area_levels("市级级代理");
}else{
list.get(i).setAdministrative_area_levels("区县级代理");
}
if (list.get(i).getAudit_status().equals("10")) {
list.get(i).setAudit_status("审核通过");
}
}
T_dictionary dictionary = new T_dictionary();
dictionary.setCode("审核状态");
t_dictionoaries = t_Dictionary_Service_Iface.selecT_dictionary(dictionary);
for(int i=0;i<list.size();i++){
String audit_status=list.get(i).getAudit_status();
for(int y =0;y<t_dictionoaries.size();y++){
if(t_dictionoaries.get(y).getValue().equals(audit_status)){
list.get(i).setAudit_status(t_dictionoaries.get(y).getDescription());
}
}
}
//获取桌面路径
FileSystemView fsv = FileSystemView.getFileSystemView();
File com=fsv.getHomeDirectory(); //桌面路径
System.out.println(com.getPath());
SimpleDateFormat dateFormat = new SimpleDateFormat("YYYYMMDDhhmmss");
String now = dateFormat.format(new Date());
//导出文件路径:默认桌面地址
String basePath = "C:/Users/Administrator/Desktop/";
//文件名
String exportFileName = agentName+""+now+".xlsx";
String[] cellTitle =title.split(",");
// 声明一个工作薄
XSSFWorkbook workBook = null;
workBook = new XSSFWorkbook();
// 生成一个表格
XSSFSheet sheet = workBook.createSheet(agentName);
// 创建表格标题行 第一行
XSSFRow titleRow = sheet.createRow(0);
for(int i=0;i<cellTitle.length;i++){
titleRow.createCell(i).setCellValue(cellTitle[i]);
}
//插入需导出的数据
for(int i=0;i<list.size();i++){
XSSFRow row = sheet.createRow(i+1); //声明行
row.createCell(0).setCellValue(i+1);
row.createCell(1).setCellValue(list.get(i).getReal_name());
row.createCell(2).setCellValue(list.get(i).getPt_id_number());
row.createCell(3).setCellValue(list.get(i).getMobile());
row.createCell(4).setCellValue(list.get(i).getFullname());
row.createCell(5).setCellValue(list.get(i).getJob_name());
row.createCell(6).setCellValue(list.get(i).getAdministrative_area_levels());
row.createCell(7).setCellValue(list.get(i).getLeave_message());
row.createCell(8).setCellValue(list.get(i).getAudit_status());
}
File file = new File(basePath+exportFileName); //输出位置
//二、 数据转成excel
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
response.setContentType("application/x-download");
exportFileName = URLEncoder.encode(exportFileName, "UTF-8"); //文件名编码转换
response.addHeader("Content-Disposition", "attachment;filename=" + exportFileName);
OutputStream out = response.getOutputStream();
workBook.write(out);
out.close();
// //文件输出流
// FileOutputStream outStream = new FileOutputStream(file);
// workBook.write(outStream);
//
// response.setContentType("application/x-download");
//// response.setContentType("application/msexcel");
//// response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
// response.addHeader("Content-Disposition", "attachment;filename="+exportFileName);
//// response.setHeader("Content-Type", "textml,charset=utf-8");
// System.out.println("导出成功!文件导出路径:--"+basePath+exportFileName);
//
// outStream.flush();
// outStream.close();
}catch (Exception e) {
e.printStackTrace();
}
}