[水晶报表]导出各种报表到指定目录

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://david.blog.csdn.net/article/details/4795317

 今天要写一ECN项目的时候需要用到导出的功能:

在网络上找了一些资料。结合自己的情况修改调试一下了 先看看效果吧

 

 

 

写到页面上了。再看看是否写到指定目录了?

OK 成功了 一下是代码

----------------------------------------------------------以下为报表加载  

     .....
string reportPath = Server.MapPath("..//Reports//project.rpt");
        //doc.Load(@"C:/test/1.rpt");
        doc.Load(reportPath);
        doc.SetDataSource(ds.Tables[0]);
        CrystalReportViewer1.ReportSource = doc;
        CrystalReportViewer1.DataBind();


----------------------------------------------------------实现生成指定格式的文件到指定目录
        

string filetype = "";
        filetype = ddl_FileFormat.SelectedValue;
        string contenttype = "";
        //string myfilename = Request.MapPath(".//") + Session.SessionID + "." + filetype;//目录地址 注意反义字符
        string myfilename = "C://test//" + Session.SessionID  +"." + filetype;
        CrystalDecisions.Shared.DiskFileDestinationOptions mydiskoptions = new CrystalDecisions.Shared.DiskFileDestinationOptions();
        mydiskoptions.DiskFileName = myfilename;
        CrystalDecisions.Shared.ExportOptions myExportOptions = new ExportOptions();//oCR.ExportOptions; 
        myExportOptions.DestinationOptions = mydiskoptions;
        myExportOptions.ExportDestinationType = CrystalDecisions.Shared.ExportDestinationType.DiskFile;
        switch (ddl_FileFormat.SelectedItem.Value) //类别
        {
            case "pdf":
                contenttype = "application/pdf";
                myExportOptions.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.PortableDocFormat;
                break;
            case "doc":
                contenttype = "application/msword";
                myExportOptions.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.WordForWindows;
                break;
            case "XLS":
                contenttype = "application/vnd.ms-excel";
                myExportOptions.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.Excel;
                break;
            case "RTF":
                contenttype = "application/rtf";
                myExportOptions.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.RichText;
                break;
                
        }
        doc.Export(myExportOptions); //输出报表 到服务器端的制定路径 
        Response.ClearContent();     //清空页面 
        Response.ClearHeaders();
        Response.ContentType = contenttype;
        Response.WriteFile(myfilename);//直接写入页面,页面平铺 可实现在线阅读功能
        Response.Flush();
        Response.Close();  

 

展开阅读全文

请教高人:水晶报表导出

06-26

我使用以下代码导出PDF(C#)rnExportOptions eptions = new ExportOptions();rnDiskFileDestinationOptions dfd = new DiskFileDestinationOptions();rndfd.DiskFileName = "c:\\1.pdf";rneptions = reportDoc.ExportOptions;rneptions.DestinationOptions = dfd;rneptions.ExportDestinationType = ExportDestinationType.DiskFile;rneptions.FormatOptions = ExportFormatType.PortableDocFormat;rnreportDoc.Export();rnResponse.ClearContent();rnResponse.ClearHeaders();rnResponse.ContentType = "application/pdf";rnResponse.WriteFile("c:\\1.pdf");rnResponse.Flush();rnResponse.Close();rnSystem.IO.File.Delete("c:\\1.pdf");rnrn结果异常:rnrn无效导出选项。 rn说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 rnrn异常详细信息: CrystalDecisions.CrystalReports.Engine.InvalidArgumentException: 无效导出选项。rnrn源错误: rnrnrn行 92: eptions.ExportDestinationType = xportDestinationType.DiskFile;rn行 93: eptions.FormatOptions = ExportFormatType.PortableDocFormat;rn行 94: reportDoc.Export();(异常)rn行 95: Response.ClearContent();rn行 96: Response.ClearHeaders();rnrn源文件: c:\inetpub\wwwroot\testcrystalreport\webform1.aspx.cs 行: 94 rnrn还有报出以下异常不知道是什么原因?rnrn异常:"文件 C:\\DOCUME~1\\MGH\\ASPNET\\LOCALS~1\\Temp\\temp_4f485cc7-1c45-451d-802d-a47dc83c3ca2.rpt 内出错:\n拒绝访问报表文件。其他程序可能正在使用它。" rn但是以前使用同样的代码没有任何问题,请高人救命啊,谢谢! 论坛

没有更多推荐了,返回首页