SpringMVC Excel导出到指定文件夹

1、导入poi-3.8.jar
2、jsp代码:

  <a id="daochu" onmousedown="daochuExcel(this);" href="">导出</a>  
<script type="text/javascript"> function daochuExcel(obj){ if(!confirm("您确定要导出吗?")) { return; } var answer= document.getElementsByName("checkbox"); var ids=''; for(var i=0;i<answer.length;i++){ if( answer[i].checked){ var ans=answer[i].value; ids+=ans+","; } $("body").append("<a id=exportExcel href='${basePath}orderController/Excelorder.do?orids="+ids+"'></a>"); document.getElementById("exportExcel").click(); $("#exportExcel").remove(); } } </script>

3、Java代码片段:
(a)

 @RequestMapping("/orderController/Excelorder")
      public void TestExcel(HttpServletRequest request, HttpServletResponse response,String orids) throws 

UnsupportedEncodingException{
          response.setContentType("octets/stream");  
       // response.addHeader("Content-Disposition", "attachment;filename=test.xls"); 
            String excelName = "订单信息表"; 
          // orids="201301051121";

            //转码防止乱码 
            response.addHeader("Content-Disposition", "attachment;filename="+new String( excelName.getBytes("gb2312"), "ISO8859- 1" )+".xls");  
            String[] headers = new String[]{"订单号","订单状态","订单金额","最终支付金额"};  
            try {  
                OutputStream out = response.getOutputStream();  
                exportExcel(excelName,headers, getList(orids), out,"yyyy-MM-dd");  
                out.close();  
                System.out.println("excel导出成功!");  
            } catch (FileNotFoundException e) {  
                    e.printStackTrace();  
            } catch (IOException e) {  
                    e.printStackTrace();  
            }  
      }

(b)

  public List<Order> getList(String orids){  
                List<Order>  orderlist= new ArrayList<Order>();

          String[] orderids=orids.split(",");
          for(int i=0;i<orderids.length;i++){
              Map<String,Object> params = new HashMap<String, Object>();
              params.put("orderid", orderids[i]);
              List<Order> olist=orderbo.QueryOrderlistOrId(params);
              orderlist.add(olist.get(0));
          }


            return orderlist;  
        }  

(c)

  protected void exportExcel(String title,String[] headers,List mapList,OutputStream out,String pattern){  
            //声明一个工作簿  
            HSSFWorkbook workbook = new HSSFWorkbook(); 
            //生成一个表格  
            HSSFSheet sheet = workbook.createSheet(title); 
            //设置表格默认列宽度为15个字符  
            sheet.setDefaultColumnWidth(20); 
            //生成一个样式,用来设置标题样式  
            HSSFCellStyle style = workbook.createCellStyle(); 
            //设置这些样式  
            style.setFillForegroundColor(HSSFColor.SKY_BLUE.index); 
            style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); 
            style.setBorderBottom(HSSFCellStyle.BORDER_THIN); 
            style.setBorderLeft(HSSFCellStyle.BORDER_THIN); 
            style.setBorderRight(HSSFCellStyle.BORDER_THIN); 
            style.setBorderTop(HSSFCellStyle.BORDER_THIN); 
            style.setAlignment(HSSFCellStyle.ALIGN_CENTER); 
            //生成一个字体  
            HSSFFont font = workbook.createFont(); 
            font.setColor(HSSFColor.VIOLET.index); 
            font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); 
            //把字体应用到当前的样式  
            style.setFont(font); 
            // 生成并设置另一个样式,用于设置内容样式  
            HSSFCellStyle style2 = workbook.createCellStyle(); 
            style2.setFillForegroundColor(HSSFColor.LIGHT_YELLOW.index); 
            style2.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); 
            style2.setBorderBottom(HSSFCellStyle.BORDER_THIN); 
            style2.setBorderLeft(HSSFCellStyle.BORDER_THIN); 
            style2.setBorderRight(HSSFCellStyle.BORDER_THIN); 
            style2.setBorderTop(HSSFCellStyle.BORDER_THIN); 
            style2.setAlignment(HSSFCellStyle.ALIGN_CENTER); 
            style2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); 
            // 生成另一个字体  
            HSSFFont font2 = workbook.createFont(); 
            font2.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL); 
            // 把字体应用到当前的样式  
            style2.setFont(font2); 
            //产生表格标题行  
            HSSFRow row = sheet.createRow(0); 
            for(int i = 0; i<headers.length;i++){ 
                HSSFCell cell = row.createCell(i); 
                cell.setCellStyle(style); 
                HSSFRichTextString text = new HSSFRichTextString(headers[i]); 
                cell.setCellValue(text); 
            }  
            for (int i=0;i<mapList.size();i++) { 
                Order o =(Order) mapList.get(i); 
                row = sheet.createRow(i+1); 
                int j = 0; 


                row.createCell(j++).setCellValue(o.getOrderid()); 
                String orderstate=o.getOrderstate();
                String str1="1";String str2="2";String str3="3";String str4="4";String str5="5";String str6="6";String str7="7";
                String orderstatestr="";
                if(orderstate.equals(str1)){
                     orderstatestr="待确认";
                 }
                 if(orderstate.equals(str2)){
                     orderstatestr="待付款";
                 }
                 if(orderstate.equals(str3)){
                     orderstatestr="待发货";
                 }
                 if(orderstate.equals(str4)){
                     orderstatestr="待收货";
                 }
                 if(orderstate.equals(str5)){
                     orderstatestr="待评价";
                 }
                 if(orderstate.equals(str6)){
                     orderstatestr="已完成";
                 }
                 if(orderstate.equals(str7)){
                     orderstatestr="关闭交易";
                 }

                row.createCell(j++).setCellValue(orderstatestr);
                row.createCell(j++).setCellValue(o.getOrderamount());
                row.createCell(j++).setCellValue(o.getIs_price_final_pay());

            }  
            try {  
                workbook.write(out); 
            } catch (IOException e) {  
                e.printStackTrace(); 
            }  
        }  

你可能感兴趣的:(Excel)