• poi 导出Excel java代码


    js:

     1 function initBatchExport(url,sub_key,current_sub_num){
     2     var btn_id="#btn_char"+current_sub_num;
     3     if(Number(current_sub_num)==1){
     4         btn_id="#btn_char";
     5     }
     6 //    查询条件
     7     //var paramObj=new Object();
     8     //var result_Data ={};
     9     month_value=$("#month_value").val();
    10     dim_key = sub_key;
    11     type = "";
    12     var exportUrl=basePath+'/amount/initBatchExport.do?month_value='+month_value+'&dim_key='+dim_key+'&type='+type+'&current_sub_num='+current_sub_num;
    13     for (var i = 1; i <=Number(current_sub_num); i++) {
    14         var parent_key = "parent_dim_"+(i);
    15         exportUrl+='&'+parent_key+'='+$("#"+parent_key).val();
    16     }
    17     $(btn_id).on("click",function(){
    18         
    19     //    var excelurl ="<%=basePath%>/hfx/unwantExport.action?impId="+impId;
    20         
    21         location.href=exportUrl;
    22     });
    23 }

    controller:

     1 @RequestMapping(value = "/initBatchExport", method = {RequestMethod.GET,RequestMethod.POST})
     2     @ResponseBody
     3     public void initBatchExport(Locale locale, Model model,HttpServletRequest request,HttpServletResponse response){
     4         ResultMap data =ResultMap.defaultResultMap(); //new HashMap<String,Object>();
     5         //Map<String,String>  param = RequestUtils.getParamsMap(request);
     6         Map<String,String>  param =new HashMap<String,String>();
     7         Map map = request.getParameterMap();
     8         String current_sub_num = request.getParameter("current_sub_num");
     9         String month_value = request.getParameter("month_value");
    10         String dim_key = request.getParameter("dim_key");
    11         String type = request.getParameter("type");
    12         for (int i = 1; i <=Integer.parseInt(current_sub_num); i++) {
    13             String parent_key = "parent_dim_"+(i);
    14             param.put(parent_key, request.getParameter(parent_key));
    15         }
    16         param.put("current_sub_num", current_sub_num);
    17         param.put("month_value", month_value);
    18         param.put("dim_key", dim_key);
    19         param.put("type", type);
    20         //需导出的数据
    21         List<ZdResult8BrandD> list = amonutService.initJqGrid(param);
    22         //初始化导出Excel
    23         // 第一步,创建一个webbook,对应一个Excel文件 
    24           HSSFWorkbook wb = new HSSFWorkbook(); 
    25           // 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet 
    26           HSSFSheet sheet = wb.createSheet("Sheet1"); 
    27           // 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short 
    28           HSSFRow row = sheet.createRow(0); 
    29           // 第四步,创建单元格,并设置值表头 设置表头居中 
    30           HSSFCellStyle style = wb.createCellStyle(); 
    31           style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式 
    32 
    33           HSSFCell cell = row.createCell(0);
    34           //'账期', '地市', '品牌','机型', '价格段', '客户','分类客户', 'subtype','value'
    35           cell.setCellValue("账期"); 
    36           cell.setCellStyle(style); 
    37           cell = row.createCell(1); 
    38           cell.setCellValue("地市"); 
    39           cell.setCellStyle(style); 
    40           cell = row.createCell(2); 
    41           cell.setCellValue("品牌"); 
    42           cell.setCellStyle(style); 
    43           cell = row.createCell(3); 
    44           cell.setCellValue("机型"); 
    45           cell.setCellStyle(style);
    46           cell = row.createCell(4); 
    47           cell.setCellValue("价格段"); 
    48           cell.setCellStyle(style);
    49           cell = row.createCell(5); 
    50           cell.setCellValue("客户"); 
    51           cell.setCellStyle(style);
    52           cell = row.createCell(6); 
    53           cell.setCellValue("分类客户"); 
    54           cell.setCellStyle(style);
    55           cell = row.createCell(7); 
    56           cell.setCellValue("子类"); 
    57           cell.setCellStyle(style);
    58           cell = row.createCell(8); 
    59           cell.setCellValue(""); 
    60           cell.setCellStyle(style);
    61 
    62           // 第五步,写入实体数据
    63           for (int i = 0; i < list.size(); i++){ 
    64                row = sheet.createRow(i + 1); 
    65                ZdResult8BrandD zdResult = list.get(i); 
    66                // 第四步,创建单元格,并设置值 
    67                row.createCell(0).setCellValue(zdResult.getStatDay()); 
    68                row.createCell(1).setCellValue(zdResult.getAreaName()); 
    69                row.createCell(2).setCellValue(zdResult.getBrandName()); 
    70                row.createCell(3).setCellValue(zdResult.getSpec()); 
    71                row.createCell(4).setCellValue(zdResult.getPriceRange()); 
    72                row.createCell(5).setCellValue(zdResult.getCustName()); 
    73                row.createCell(6).setCellValue(zdResult.getCustStage()); 
    74                row.createCell(7).setCellValue(zdResult.getSubType()); 
    75                row.createCell(8).setCellValue(zdResult.getValue()); 
    76 //               cell = row.createCell(3); 
    77 //               cell.setCellValue(new SimpleDateFormat("yyyy-MM-dd").format(stu.getTime())); 
    78               }
    79 
    80         //第六步,输出Excel文件
    81           try {
    82           OutputStream output=response.getOutputStream();
    83           response.reset();
    84           SimpleDateFormat df = new SimpleDateFormat("yyyy_MM_dd HHmmss");//设置日期格式
    85           String fileName = dim_key+df.format(new Date());// new Date()为获取当前系统时间
    86           response.setHeader("Content-disposition", "attachment; filename="+fileName+".xls");
    87           response.setContentType("application/msexcel");  
    88         
    89             wb.write(output);
    90             output.flush();
    91         } catch (IOException e) {
    92             e.printStackTrace();
    93         }
    94           
    95 
    96         //return resultMap;
    97     }
  • 相关阅读:
    微信公众号--JS-SDK
    微信公众号--网页授权
    微信公众号--资源请求
    微信公众号--相关资料
    微信公众号--消息回复
    一.小程序 --申请
    2017年总结
    SSM框架中常用的配置文件
    UML的9种图例解析
    23种设计模式汇总整理
  • 原文地址:https://www.cnblogs.com/yxdmoodoo/p/10021136.html
Copyright © 2020-2023  润新知