您的位置:首页 > 其它

WinForm实现PUSH模式的报表功能

2015-07-18 23:32 169 查看
最近在实现一个报表功能的时候,遇到如下问题:

System.Exception:加载报表失败。

System.Runtime.InteropServices.COMException(0x80004005):系统找不到指定的路径

通过异常信息可知,主要是报表文件查找不到导致的,接下来就另写了一段代码,主要是获取报表文件的路径。

如下:

reportPath = Application.StartupPath.Substring(0, Application.StartupPath.Substring(0,Application.StartupPath.LastIndexOf(“\”)).LastIndexOf(“\”));

reportPath += @”\文件夹名称\” + 报表名称;

运行程序,结果还是报以上问题,自己就通过调试,查看到获取的报表路径是OK的,到这郁闷了,真不知道是怎么回事了,自己只好再次求助于互联网,皇天不负有心人,最后还是让我找到相应的解决办法,自己在将自己的简单总结贴出来,希望对有需要的人有帮助。

1、我这里使用的是PUSH模式,因此第一步是新建一个数据集dsProReport,具体的方式我就不在这里说明了

2、新建一个报表crpProReport.rpt,将数据集dsProReport做为报表数据源

3、通过代码实现相应的报表显示功能,如下:

//实例化数据集
dsProReport ds = new dsProReport();
//通过GetList获取相应的数据集,并赋值我ds
ds= GetList(dtpStart.Text, dtpEnd.Text, "ProjectName,Stage ", "Report");
//实例化ReportDocument 对象
ReportDocument myReportDoc = new ReportDocument();
//下载客户端到本地的应用程序路径;
string AppPath = Application.StartupPath;
string RptFileFullName = AppPath + @"\crpProReport.rpt";
//取得Resources资源文件下的报表文件
ResourceManager resources = new ResourceManager(typeof(ProManagerSystem.Properties.Resources));
//只写报表名称,不带后缀
byte[] bytes = (byte[])resources.GetObject("crpProReport");
if (File.Exists(RptFileFullName))
File.Delete(RptFileFullName);
FileStream fileStream = new FileStream(RptFileFullName, FileMode.CreateNew);
BinaryWriter binWriter = new BinaryWriter(fileStream);
binWriter.Write(bytes, 0, bytes.Length);
binWriter.Close();
fileStream.Close();
string reportPath = RptFileFullName;
//加载报表文件
myReportDoc.Load(reportPath);
//绑定数据源
myReportDoc.SetDataSource(ds);
crpView.ReportSource = myReportDoc;


注:前面由于获取不到报表文件的路径,而导致加载报表失败的解决方式如下:

1、将该报表文件添加至Resources资源文件–打开Properties,双击下方的Resource.resx,点击“添加资源”–“添加现有文件”

2、通过相应的代码获取报表文件的路径,见上方相应的代码:
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: