使用ReportViewer中的默认导出按钮时,是否可以简单地提示用户打开导出的报告?我查看了ReportExport事件,尽管在导出之前会触发它。我唯一能想到的是取消ReportExport并创建我自己的导出功能,但我希望我不需要这样做。导出后是否有任何我失踪的事件?
答案 0 :(得分:4)
我找到了解决方案。 @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 "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);
}
}
答案 1 :(得分:1)
根据各种帖子和资源{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);
希望有所帮助。