【问题标题】:Winforms ReportViewer and Open After ExportWinforms ReportViewer 并在导出后打开
【发布时间】:2011-11-30 18:31:13
【问题描述】:

当使用 ReportViewer 中的默认导出按钮时,有没有办法简单地提示用户打开导出的报表?我查看了 ReportExport 事件,尽管它在导出发生之前触发。我唯一能想到的就是取消 ReportExport 并创建我自己的导出功能,尽管我希望我不需要这样做。导出后是否有任何我错过了火灾的事件?

【问题讨论】:

    标签: winforms visual-studio-2010 c#-4.0 report-viewer2010


    【解决方案1】:

    我找到了解决方案。 @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);
        }
    }
    

    【讨论】:

    • 谢谢,工作很棒。我刚刚为 excel xlsx 格式添加了这个额外的案例case "EXCELOPENXML": return ".xlsx";
    【解决方案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 互操作的引用并通过以下方式调用 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);
    

    希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-30
      • 1970-01-01
      • 1970-01-01
      • 2019-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-02
      • 1970-01-01
      相关资源
      最近更新 更多