MecGrid导出Excel

     MecGrid是一个开源的报表组件,似乎是小日本写的,功能挺强大的就是不公布源代码,官方提供的导出Excel功能只是Air专用的(因为web的flex程序没有File类),看了下代码后觉得web也是可以实现的。

    具体的思路就是把生成的二进制数据传到服务器端,由服务器生成文件。

    具体的代码如下

   前台:

Js代码 复制代码  收藏代码
  1. private function makeExcelFile(event:Event):void  
  2. {   
  3.     var exp:MecExporter = new MecExporter();   
  4.     exp.charset = "UTF-8";   
  5.     exp.AddDataGrid(mgrid, "");   
  6.     var ebt:ByteArray = exp.Export2BiffExcel();   
  7.     var param:Object=new Object();   
  8.     param.data=ebt;   
  9.     param.path="tools";   
  10.     param.fileName="mecgrid";   
  11.     JdbcService.getInstance(true).callfunc("UserService","exportExcel",param,null,null);   
  12. //  var f:File= event.target as File;   
  13. //  var fs:FileStream = new FileStream();   
  14. //  fs.open(f, FileMode.WRITE);   
  15. //  fs.writeBytes(ebt);   
  16. //  fs.close();   
  17. }  

 后台:

Java代码 复制代码  收藏代码
  1. /**  
  2.      * Creates the excel.MECGrid导出Excel  
  3.      *   
  4.      * @param param the param  
  5.      *   
  6.      * @return the string 导出成功返回文件的路径否则返回null  
  7.      */  
  8.     public String createExcel(ASObject param){   
  9.         String path=(String) param.get("path");   
  10.         String fileName=(String)param.get("fileName");   
  11.         String uri=FlexContext.getServletContext().getRealPath("");   
  12.         //1 导出文件保存的路径 相对路径   
  13.         String inPath=uri+"/upload/"+path+"/"+fileName;   
  14.         //2 MecGrid生成的数据信息   
  15.         byte[] b=(byte[]) param.get("data");   
  16.         BufferedOutputStream stream = null;   
  17.         File file = null;   
  18.         try {   
  19.             logger.debug("导出Excel路径"+inPath);   
  20.             file = new File(inPath);   
  21.             FileOutputStream fstream = new FileOutputStream(file);   
  22.             stream = new BufferedOutputStream(fstream);   
  23.             stream.write(b);   
  24.         } catch (Exception e) {   
  25.             logger.debug(e.getMessage(),e);   
  26.             logger.info("导出excel时发生异常");   
  27.             return null;   
  28.         } finally {   
  29.             if (stream != null) {   
  30.                 try {   
  31.                     stream.close();   
  32.                 } catch (IOException e1) {   
  33.                     logger.debug(e1.getMessage(),e1);   
  34.                 }   
  35.             }   
  36.         }   
  37.         return inPath;   
  38.     }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值