码迷,mamicode.com
首页 > 编程语言 > 详细

poi 导出Excel java代码

时间:2018-11-26 17:24:28      阅读:134      评论:0      收藏:0      [点我收藏+]

标签:app   stream   数列   integer   price   tag   export   isp   ret   

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     }

 

poi 导出Excel java代码

标签:app   stream   数列   integer   price   tag   export   isp   ret   

原文地址:https://www.cnblogs.com/yxdmoodoo/p/10021136.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!