下载

HttpServletResponse response;

//导出excel字段列表数组

String[] header = new String[]{"日期","下单笔数","下单金额(元)", "退单笔数", "退单金额(元)"};

//对应字段数组
String[] attributes = new String[]{"clearDate", "consumeCount", "consumeTotalAmt", "returnCount", "returnTotalAmt"};

//数据库中查询要导出的结果
List<BillsCheckDTO> listdata = (List<BillsCheckDTO>) model.get("dataList");

// 文件导出流   

OutputStream output = null;  

try {  

     //要导出的文件名 

     String filename = year + "年" + month + "月_" + "日对账单.xls";

     // 编码防止下载后的文件名乱码   

     filename = URLEncoder.encode(filename, "UTF-8");

     //设定文件导出格式

     response.addHeader("Content-Disposition", "attachment;filename=" + filename);  

     response.setContentType("application/vnd.ms-excel;charset=UTF-8");  

     try {    

            //实例化导出流

            output = new BufferedOutputStream(response.getOutputStream());  

            创建导出excel

            ExportExcel<T> ex = new ExportExcel<T>();    

            //传入导出数据

            ex.exportExcel(header, attributes, "yyyy年MM月dd日",listdata, output);

            //执行

            output.flush();  

     } catch (Exception e) {   

            e.printStackTrace();  

     } finally {   

            //关闭文件流

            if (output != null) {      

                   try {     

                          output.close();      

                   } catch (IOException e) {  

                          logger.error(e);    

                   }     

                  output = null;    

              }    

      }

  } catch (Exception e) {   

        //异常提示

        logger.error("export file exception", e);  

 }

posted @ 2015-11-23 18:12  神玄晓  阅读(122)  评论(0编辑  收藏  举报