MecGrid是一个开源的报表组件,似乎是小日本写的,功能挺强大的就是不公布源代码,官方提供的导出Excel功能只是Air专用的(因为web的flex程序没有File类),看了下代码后觉得web也是可以实现的。
具体的思路就是把生成的二进制数据传到服务器端,由服务器生成文件。
具体的代码如下
前台:
private function makeExcelFile(event:Event):void { var exp:MecExporter = new MecExporter(); exp.charset = "UTF-8"; exp.AddDataGrid(mgrid, ""); var ebt:ByteArray = exp.Export2BiffExcel(); var param:Object=new Object(); param.data=ebt; param.path="tools"; param.fileName="mecgrid"; JdbcService.getInstance(true).callfunc("UserService","exportExcel",param,null,null); // var f:File= event.target as File; // var fs:FileStream = new FileStream(); // fs.open(f, FileMode.WRITE); // fs.writeBytes(ebt); // fs.close(); }
后台:
/**
* Creates the excel.MECGrid导出Excel
*
* @param param the param
*
* @return the string 导出成功返回文件的路径否则返回null
*/
public String createExcel(ASObject param){
String path=(String) param.get("path");
String fileName=(String)param.get("fileName");
String uri=FlexContext.getServletContext().getRealPath("");
//1 导出文件保存的路径 相对路径
String inPath=uri+"/upload/"+path+"/"+fileName;
//2 MecGrid生成的数据信息
byte[] b=(byte[]) param.get("data");
BufferedOutputStream stream = null;
File file = null;
try {
logger.debug("导出Excel路径"+inPath);
file = new File(inPath);
FileOutputStream fstream = new FileOutputStream(file);
stream = new BufferedOutputStream(fstream);
stream.write(b);
} catch (Exception e) {
logger.debug(e.getMessage(),e);
logger.info("导出excel时发生异常");
return null;
} finally {
if (stream != null) {
try {
stream.close();
} catch (IOException e1) {
logger.debug(e1.getMessage(),e1);
}
}
}
return inPath;
}
备注:刚开始在网上查了些资料,网上有人说只能用air生成excel文件,看了官方的例子后,觉得可以实现web导出excel的。