转自:https://stackoverflow.com/questions/7649699/winforms-reportviewer-and-open-after-export
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?
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.
this.reportViewer1.ReportExport += new ExportEventHandler(this.ReportViewer1_ReportExport);
Here's my ReportExport event handling method:
private void ReportViewer1_ReportExport(object sender, ReportExportEventArgs e)
{
e.Cancel = true;
string extension = this.GetRenderingExtension(e.Extension);
SaveFileDialog saveFileDialog = new SaveFileDialog()
{
Title = "Save As",
CheckPathExists = true,
InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
Filter = e.Extension.LocalizedName + " (*" + extension + ")|*" + extension + "|All files(*.*)|*.*",
FilterIndex = 0
};
if (saveFileDialog.ShowDialog(this) == DialogResult.OK)
{
this.reportViewer1.ExportDialog(e.Extension, e.DeviceInfo, saveFileDialog.FileName);
// Here's where I call my method to prompt user to open the file.
RadExportHelper.OpenFileWithPrompt(saveFileDialog.FileName);
}
}
The RenderingExtension class doesn't publicly expose the actual file extensions that are exported, so I created this method:
private string GetRenderingExtension(RenderingExtension extension)
{
switch (extension.Name)
{
case "PDF":
return ".pdf";
case "CSV":
return ".csv";
case "EXCEL":
return ".xls";
case "MHTML":
return ".mhtml";
case "IMAGE":
return ".tif";
case "XML":
return ".xml";
case "WORD":
return ".doc";
case "HTML4.0":
return ".html";
case "NULL":
throw new NotImplementedException("Extension not implemented.");
}
throw new NotImplementedException("Extension not implemented.");
}
Lastly, here's my helper method to prompt the user and open the file if they choose:
public static void OpenFileWithPrompt(string file)
{
if (RadMessageBox.Show(
Resources.RadHelper_OpenExportedDataMessage,
Resources.RadHelper_OpenExportedDataTitle,
MessageBoxButtons.YesNo,
RadMessageIcon.Question,
MessageBoxDefaultButton.Button1) == DialogResult.Yes)
{
Process.Start(file);
}
}
answered Oct 4 '11 at 20:58
11.1k77 gold badges4848 silver badges7777 bronze badges
-
Thanks, worked awesome. I just added this extra case for excel xlsx format
case "EXCELOPENXML": return ".xlsx";
– Ben Winding Feb 9 '16 at 0:48
add a comment
1
According to a variety of posts and resources {1,2,3}, what you are trying to accomplish is not a built in functionality of the ReportViewer control in Visual Studio.
If this functionality is essential, you could always disable the export button on the report viewer and add a button or other control to take care of the exporting. Below is a class call to a method that I use in a program to auto-generate an excel file when a report is run, but the only change you would have to make would be to subscribe to this method via button click:
Side note: custNmbr is a variable that is used to name the report after the customer it was run for. You may remove this if you like (as it is a report parameter of mine), or make it dynamic via your own code to make sure the files don't overwrite one another.
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);
}
}
Since Dpath would be the location of this newly exported file, you could simply Add a reference to the Excel Interop and call excel/the new file via:
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);
Hope that helps.
使用ReportViewer中的默认导出按钮时,是否可以简单地提示用户打开导出的报表?我查看了ReportExport事件,尽管此事件在导出之前触发。我唯一能想到的就是取消ReportExport并创建自己的导出功能,尽管我希望我不需要这样做。导出后是否有任何我错过的火警事件?
我找到了解决方案。@KreepN,在各个讨论区中,我在网上都看到了与您类似的解决方案,但是,我发现了另一个解决方案,可以更好地满足我的需求。这提供了所有默认的导出功能。这是我所做的:
首先,在创建表单时订阅ReportExport事件。
this.reportViewer1.ReportExport += new ExportEventHandler(this.ReportViewer1_ReportExport);
这是我的ReportExport事件处理方法:
private void ReportViewer1_ReportExport(object sender, ReportExportEventArgs e)
{
e.Cancel = true;
string extension = this.GetRenderingExtension(e.Extension);
SaveFileDialog saveFileDialog = new SaveFileDialog()
{
Title = "Save As",
CheckPathExists = true,
InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
Filter = e.Extension.LocalizedName + " (*" + extension + ")|*" + extension + "|All files(*.*)|*.*",
FilterIndex = 0
};
if (saveFileDialog.ShowDialog(this) == DialogResult.OK)
{
this.reportViewer1.ExportDialog(e.Extension, e.DeviceInfo, saveFileDialog.FileName);
// Here's where I call my method to prompt user to open the file.
RadExportHelper.OpenFileWithPrompt(saveFileDialog.FileName);
}
}
RenderingExtension类未公开公开导出的实际文件扩展名,因此我创建了以下方法:
private string GetRenderingExtension(RenderingExtension extension)
{
switch (extension.Name)
{
case "PDF":
return ".pdf";
case "CSV":
return ".csv";
case "EXCEL":
return ".xls";
case "MHTML":
return ".mhtml";
case "IMAGE":
return ".tif";
case "XML":
return ".xml";
case "WORD":
return ".doc";
case "HTML4.0":
return ".html";
case "WORDOPENXML":
return ".docx";
case "EXCELOPENXML":
return ".xlsx";
case "NULL":
throw new NotImplementedException("Extension not implemented.");
}
throw new NotImplementedException("Extension not implemented.");
}
最后,这是我的帮助程序方法,如果用户选择,它会提示用户并打开文件:
public static void OpenFileWithPrompt(string file)
{
if (RadMessageBox.Show(
Resources.RadHelper_OpenExportedDataMessage,
Resources.RadHelper_OpenExportedDataTitle,
MessageBoxButtons.YesNo,
RadMessageIcon.Question,
MessageBoxDefaultButton.Button1) == DialogResult.Yes)
{
Process.Start(file);
}
}
谢谢,工作很棒。我刚刚为excel xlsx格式添加了这种额外情况case "EXCELOPENXML": return ".xlsx";
–
根据各种职位和资源{的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);
希望能有所帮助。