在web应用程序中,我们无法像在windows应用程序那样导出报表,因为程序是在服务器端执行的,执行导出时,其结果也是在服务器端,那应该如何才能实现完整的客户端导出呢?其实这个也不难,方法是:把报表指定导出到某个网站上事先建立好的报表暂存文件,然后利用response.redirect()指令,将浏览器网址指向该报表位置,这样用户的浏览器就会尝试下载刚导出的文件,文件就会被下载到客户端,从而实现我们需要的效果。 部分代码如下:
public string exportreport()
{
exportoptions creo = new exportoptions();
diskfiledestinationoptions crdo = new diskfiledestinationoptions();
string filename = request.physicalapplicationpath + “exportfile\exap.xls”;
//设置导出选项
creo = myrpt.exportoptions;
creo.exportformattype = exportformattype.excel;
creo.exportdestinationtype = exportdestinationtype.diskfile;
//设置磁盘文件选项
crdo.diskfilename = filename;
creo.destinationoptions = crdo;
//导出报表
myrpt.export();
return filename;
}
private void buttonexport_click(object sender, system.eventargs e)
{
string filename = exportreport();
response.redirect(replace(filename,request.physicalapplicationpath + “exportfile\”,””));
}
要注意的是:当在web中进行导出时,需要对导出目录具有建立文件的权限,如果权限不足,将会出现“拒绝访问报表文件……”的错误。 让aspnet用户(安装.net framework时自动生成的系统用户)在导出目录文具有“写入”权限即可。
