java export xlsx format excel, prompted browser version


// **************************** *********** Description ************************************** ********************
     commented before are taking detours, can be exported, but the browser does not download prompt, did not respond, the user experience is not good, mainly changed the output stream, the browser now has a successful download prompt.

// **************************** *********** js Code ************************************* *********************
function exportExcel () {
    var Codes = [];
    . AreaId var = $ ( "Area #") Val ();
    var = cityId $ ( "# City") Val ();.
    var provinceId = $ ( "# Province") Val ();.
    $ .each ($ ( 'INPUT: CheckBox: the checked'), function () {
        codes.push ( $ (the this) .val ());
    });
    
    var title = "number, user ID number, telephone, administrative, office, district level, messages, audit status";
    
// 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 ( "download success");


    


// **************************** ************************************************** ***********************************
/ **
     * background code - the code behind - the code behind - - background code - the code behind - background code - the code behind -
     * @param the Response must be the
     * @param applyEntrepreneurialInfo I need sql query parameters
     * @param title the column name
     * @param agentName for the file name and sheet name
     * /
    // export the agents approved by the information
    @ RequestMapping (value = "provinceExportExcel", Method, = RequestMethod.GET)
    @ResponseBody
    public void provinceExportExcel (HttpServletResponse the Response, applyEntrepreneurialInfo applyEntrepreneurialInfo, String title, String AgentName) {
        the try {
            // prepare data
            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 ( "District county agent");
                }
                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 ());
                    }
                }
                
            }
            
            // Get the path table
            FileSystemView FileSystemView.getFileSystemView FSV = ();
            File fsv.getHomeDirectory COM = (); // table path
            System.out.println (com.getPath ());
            
            SimpleDateFormat dateFormat = new new SimpleDateFormat ( "YYYYMMDDhhmmss");
            String = dateFormat.format now (new new a Date ());
            // export the file path: the default desktop address 
            String basePath = "C: / the Users / Administrator / Desktop /";  
            // filename
            String exportFileName = agentName + "" + now + "xlsx";.
            String [] = cellTitle title.split ( ",");
            
            // declare a workbook
            XSSFWorkbook WorkBook = null;
            WorkBook = new new XSSFWorkbook ();
            // generate a table
            XSSFSheet Sheet = workBook.createSheet (AgentName);
            // create form a first row header row
            XSSFRow titleRow = sheet.createRow (0);
            for (int I = 0; I <cellTitle.length; I ++) {
                titleRow.createCell (I) .setCellValue (cellTitle [I]);
            }
            // insert derived data required
            for (int I = 0; I <list.size (); I ++) {
                XSSFRow sheet.createRow row = (I +. 1); // declaration line
                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();
        }
    }

Published 66 original articles · won praise 8 · views 130 000 +

Guess you like

Origin blog.csdn.net/qq_37889636/article/details/81135642