获取easyui datagrid表头和数据 动态生成excel表格

js

function importexecttogerenbiangen(){
    var $bodydiv = $("div[name='gerenkehuyewu']");
    var $table = $bodydiv.find("table[name='dg']");
     var rows  = $table.datagrid("getRows");
     if(rows.length==0){  
         $.messager.alert("提示", "下载数据不能为空!", 'info');
         return false;  
     }
     var opts = $table.datagrid('getColumnFields');
     var table=[];
     for(var i=0;i<opts.length;i++){

var tablename=$table.datagrid('getColumnOption',opts[i]).title+","+$table.datagrid('getColumnOption',opts[i]).field;

         table.push(tablename);
     }//获得title和field方便后台对应
     rows = JSON.stringify(rows);
     var month = $bodydiv.find("input[name='month']").val();  
     $.messager.confirm('确认', '确认把该搜索结果导出Excel表格 ?', function(r) {
            if (r) {
                var url = 'xxx?rows='+rows+'&table='+table
                window.location.href=url;
            }
        });

}

controller

@RequestMapping(value = "xxx")
        @ResponseBody
        public ModelAndView xxx(String rows,String[] table) {
            List<TongjiForGerenXuQian> gerenyewureport = Json2Bean.getListBean(rows, TongjiForGerenXuQian.class);
             Map<String, String> ziDuan = new HashMap<String, String>();
             for(int i=0;i<table.length;i++){
                 ziDuan.put(0+"-"+table[1], table[0]);    
                 if(i%2==0){
                     ziDuan.put(i/2+"-"+table[i+1], table[i]);    
                 }
                

             }

             Map<String, Object> model = new HashMap<String, Object>();
             model.put("listData", gerenyewureport);
             model.put("ziDuan", ziDuan);
            System.out.println(BeanJson.bean2json(gerenyewureport));
            return new ModelAndView(new ExcelForGerenYewuReport(), model);
        }

ExcelForGerenYewuReport

@SuppressWarnings("deprecation")
public class ExcelForGerenYewuReport extends AbstractExcelView{
    @SuppressWarnings("unchecked")
    @Override
    protected void buildExcelDocument(Map<String, Object> model, HSSFWorkbook workbook,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        String excelName = "xxx.xls";
        // 设置response方式,使执行此controller时候自动出现下载页面,而非直接使用excel打开
        response.setContentType("APPLICATION/OCTET-STREAM");
//        response.setHeader("Content-Disposition", "attachment; filename="+ URLEncoder.encode(excelName, "UTF-8"));
        
        response.setHeader("Content-Disposition", "attachment; filename="+ new String(excelName.getBytes("GBK"),"ISO8859-1"));    
        
        Map<String, String> ziDuan = (Map<String, String>) model.get("ziDuan");
        List<TongjiForGerenXuQian> listData = (List<TongjiForGerenXuQian>) model.get("listData");
        Object[] keys = ziDuan.keySet().toArray();
        String[] ziDuanKeys = new String[keys.length];
        for (int k = 0; k < keys.length; k++) {
            String temp = keys[k].toString();
            int xuHao = Integer.valueOf(temp.substring(0, 1));
            ziDuanKeys[xuHao] = temp.substring(temp.indexOf("-")+1,temp.length());
        }
        //单元格样式
        HSSFCellStyle headStyle = ExcelUtil.createCellStyle(workbook,(short)18,true,1);//表头标题行样式
        //合并单元格CellRangeAddress构造参数依次表示起始行,截至行,起始列, 截至列
                CellRangeAddress crdHeadTitle = new CellRangeAddress(0,0,0,ziDuanKeys.length-1);//表头标题合并单元格
        //创建一个sheet工作表
        HSSFSheet sheet = workbook.createSheet("xxx");
        sheet.addMergedRegion(crdHeadTitle);
        //创建表头标题行
        HSSFRow headtitlerow = sheet.createRow(0);  
        ExcelUtil.addCell(headtitlerow, 0,  headStyle, "xxx");
        HSSFRow headerrow = sheet.createRow(1);
        for (int k = 0; k < ziDuanKeys.length; k++) {
             HSSFCell cell = headerrow.createCell(k);
                 sheet.setColumnWidth(k, 6000);
                 cell.setCellValue(new HSSFRichTextString(ziDuan.get(k
                 +"-"+ ziDuanKeys[k])));
             }
        //创建列标题;并且设置列标题
         for (int i = 0; i < listData.size(); i++) {
             HSSFRow row = sheet.createRow(2+i);  
             Object obj = listData.get(i);
                for (int j = 0; j < ziDuanKeys.length; j++) {
                    HSSFCell cell1 = row.createCell(j);
                        String ziDuanName = (String) ziDuanKeys[j];
                        System.out.println(ziDuanName);
                        ziDuanName = ziDuanName.replaceFirst(ziDuanName
                        .substring(0, 1), ziDuanName.substring(0, 1)
                        .toUpperCase());
                        ziDuanName = "get" + ziDuanName;
                        Class clazz = Class.forName(obj.getClass().getName());
                        Method[] methods = clazz.getMethods();
                        Pattern pattern = Pattern.compile(ziDuanName);
                        Matcher mat = null;
                        for (Method m : methods) {
                            mat = pattern.matcher(m.getName());
                            if (mat.find()) {
                            Object shuXing = m.invoke(obj, null);
                            if (shuXing != null) {
                                cell1.setCellValue(shuXing.toString());//这里可以做数据格式处理
                            } else {
                                cell1.setCellValue("");
                            }
                            break;
                        }
                    }
                }
            
        }
        
    }
   
}


阅读更多

没有更多推荐了,返回首页