导出功能代码

//html页面

<div class="btn button_hover button_xz" style="90px" onclick="exportExcel()">
    导出
</div>


function exportExcel(){

$.fileDownload("http://syrk.hubei.gov.cn/api/hjdz/DmxxController/dzglExport",{ }).done(function () { }).fail(function () { });
}

  //controller层

    /**
     * <p>Description:建筑物信息导出</p >
     * 
     * 
     */
    @GetMapping("/dzglExport")
    public void dzglExport(HttpServletResponse response, HttpSession session)throws IOException {
        Map<String, Object> map = new HashMap<String, Object>();
        OutputStream out = null;
        try {
            List<Map<String, Object>> mpglList = dmxxService.dzglExport(map);
            out = response.getOutputStream();

            String fileName = "地址管理.xls";
            response.setCharacterEncoding("GBK");
            response.addHeader("Content-Disposition","attachment;filename=" + new String(fileName.getBytes("GBK"), "ISO8859-1"));

            exportXqgl("地址管理",mpglList, out);
        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            if(out != null){
                out.flush();
                out.close();
            }
        }
    }

    /**
     * <p>Description:信息导出公用方法</p >
     * 
     *
     */
    public void exportXqgl(String title, List<Map<String,Object>> dataset,OutputStream out) throws IOException {
        Map<String,Object> map = null;
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet(title);
        HSSFFont font = workbook.createFont();
        font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); //加粗
        HSSFCellStyle cellStyle= workbook.createCellStyle();
        cellStyle.setFont(font);
        //cellStyle.setWrapText(true);
        cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
        sheet.setDefaultColumnWidth((short) 10);

        String[] h1 = {"地址编号","地址名称"};
        HSSFRow row = sheet.createRow(0);
        row.setHeight((short)330);
        HSSFRow row1 = sheet.createRow(1);
        row.setHeight((short)330);
        for(int i=0;i<h1.length;i++){
            HSSFCell cell = row.createCell(i);
            cell.setCellStyle(cellStyle);
            cell.setCellValue(h1[i]);
        }


        HSSFCellStyle cellStyle2= workbook.createCellStyle();
        cellStyle2.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 居中

        String[] dataTitles = {"DZBH","ZZQC"};
        for(int i=0; i < dataset.size(); i++){
            row = sheet.createRow(i + 1);
            map = dataset.get(i);
            for(int j=0; j < dataTitles.length; j++){
                HSSFCell cell = row.createCell(j);
                cell.setCellStyle(cellStyle2);
                if("LSDZ_PDBS".equals(dataTitles[j]) ||"YXLH_PDBS".equals(dataTitles[j]) ||"SHJZ_PDBS".equals(dataTitles[j]) ||"DZZCZBS".equals(dataTitles[j]) ||"DZZZYBS".equals(dataTitles[j])){
                    String s = map.get(dataTitles[j]).toString();
                    if(null!=s){
                        if("1".equals(map.get(dataTitles[j]).toString())){
                            cell.setCellValue("是");
                        }else if("0".equals(map.get(dataTitles[j]).toString())){
                            cell.setCellValue("否");
                        }
                    }
                }else {
                    cell.setCellValue(transNull(map.get(dataTitles[j])));
                }


            }
        }
        workbook.write(out);
    }

    /**
     * <p>Description:对象非空校验</p >
     * 
     * 
     */
    public String transNull(Object o){
        return o == null ? "" : o.toString();
    }

   //servixe  和mapper 层

 @Override
    @ReadDataSource
    public List<Map<String, Object>> dzglExport(Map<String, Object> map) {
        return dmxxMapper.dzglExport(map);
    }





@Select("select * from  T_DZGL_DZXX")
    List<Map<String, Object>> dzglExport(Map<String, Object> map);

  

原文地址:https://www.cnblogs.com/qianyuhebaobao/p/11213372.html