显示已转换的报告视图报告

时间:2014-12-22 18:15:42

标签: winforms pdf visual-studio-2012 reportviewer filestream

我正在尝试显示转换为PDF的报告。我找到了一些显示PDF的代码,但它需要存储在磁盘上。有没有办法暂时在本地存储PDF,以便读者可以调用它?

以下是打印按钮的当前代码。

namespace Dispatch311.Views

/// <summary>
/// Interaction logic for PrintDialog.xaml
/// </summary>
public partial class PrintDialog : Window
{
    public PrintDialog()
    {
        InitializeComponent();                       
    }
    public void DisplayReport(int eventID)
    {
        Warning[] warnings;
        string[] streamids;
        string mimeType;
        string encoding;
        string filenameExtention;

        reportViewer.ProcessingMode = Microsoft.Reporting.WinForms.ProcessingMode.Remote;
        ServerReport serverReport = reportViewer.ServerReport;
        reportViewer.ShowParameterPrompts = false;
        serverReport.ReportServerUrl = new Uri("http://sql2008test/reportserver");
        serverReport.ReportPath = "/311Reports/311SingleEvent";
        ReportParameter ID = new ReportParameter();
        ID.Name = "ID";
        ID.Values.Add(eventID.ToString());
        reportViewer.ServerReport.SetParameters(
            new ReportParameter[] { ID });

        byte[] bytes = reportViewer.ServerReport.Render(
            "PDF", null, out mimeType, out encoding, out filenameExtention,
            out streamids, out warnings);

        using (FileStream fs = new FileStream("EventPDF.pdf", FileMode.Create))
        {
            fs.Write(bytes, 0, bytes.Length);
        }

        reportViewer.RefreshReport();
    }


}

1 个答案:

答案 0 :(得分:0)

修复了代码的结尾,看起来像这样:

byte[] bytes = reportViewer.ServerReport.Render("PDF",
            null, out mimeType, out encoding, out filenameExtension, out streamids, out warnings);
        using (FileStream fs = new FileStream("311Event.pdf", FileMode.Create))
        {
            fs.Write(bytes, 0, bytes.Length);
        }
        string fileName = "311Event.pdf";
        System.Diagnostics.Process process = new System.Diagnostics.Process();
        process.StartInfo.FileName = fileName;
        process.Start();
        process.WaitForExit();

这样就可以弹出Adobe Reader而不是reportviewer,并在用户退出阅读器时返回应用程序。