今天要写一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();