Winforms ReportViewer 和导出后打开
当使用 ReportViewer 中的默认导出按钮时,是否有一种方法可以简单地提示用户打开导出的报表?我查看了 ReportExport 事件,尽管该事件在导出发生之前触发。我唯一能想到的就是取消 ReportExport 并创建我自己的导出功能,尽管我希望我不需要这样做。导出后是否有任何我错过的火灾事件?
When using the default export buttons within the ReportViewer, is there a way to simply prompt the user to open the exported report? I looked at the ReportExport event, though this fires before the export occurs. The only thing I can think of is to cancel the ReportExport and create my own export functionality, though I hope I do not need to do this. Are there any events that I'm missing that fire after the export occurs?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

发布评论
评论(2)
根据各种帖子和资源{1,2,3},您想要完成的不是Visual Studio 中 ReportViewer 控件的内置功能。
如果此功能是必需的,您可以随时禁用报表查看器上的导出按钮,并添加按钮或其他控件来处理导出。下面是对一个方法的类调用,我在程序中使用该方法在运行报告时自动生成 Excel 文件,但您需要做的唯一更改是通过单击按钮订阅此方法:
旁注: custNmbr 是一个变量,用于根据运行报告的客户来命名报告。如果您愿意,您可以删除它(因为它是我的报告参数),或者通过您自己的代码使其动态化,以确保文件不会相互覆盖。
public static void reportWriter(ReportViewer reportViewer1, string custNmbr)
{
Warning[] warnings;
string[] streamids;
string mimeType;
string encoding;
string filenameExtension;
string Dpath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + custNmbr + ".xls";
byte[] bytes = reportViewer1.LocalReport.Render(
"Excel", null, out mimeType, out encoding, out filenameExtension,
out streamids, out warnings);
using (FileStream fs = new FileStream(Dpath, FileMode.Create))
{
fs.Write(bytes, 0, bytes.Length);
}
}
由于 Dpath 将是这个新导出文件的位置,因此您可以简单地添加对 Excel Interop 的引用并通过以下方式调用 excel/新文件:
Application excel = new Application();
Workbook wb = excel.Workbooks.Open(Dpath, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
希望有帮助。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
我找到了一个解决方案。 @KreepN,我在各种讨论板上看到了与您类似的在线解决方案,但是,我找到了另一个更适合我所寻找的解决方案。这提供了所有默认的导出功能。这就是我所做的:
首先,在创建表单时订阅 ReportExport 事件。
这是我的 ReportExport 事件处理方法:
RenderingExtension 类不会公开公开导出的实际文件扩展名,因此我创建了此方法:
最后,这是我的辅助方法,用于提示用户并打开文件(如果他们选择):
I found a solution for this. @KreepN, I had seen similar solutions to yours online throughout various discussion boards, however, I've found another solution which better suites what I was looking for. This provides all of the default functionality for exporting. Here's what I did:
First, subscribe to the ReportExport event when form is created.
Here's my ReportExport event handling method:
The RenderingExtension class doesn't publicly expose the actual file extensions that are exported, so I created this method:
Lastly, here's my helper method to prompt the user and open the file if they choose: