【问题标题】:View RDLC Report as pdf in Asp.net MVC在 Asp.net MVC 中以 pdf 格式查看 RDLC 报告
【发布时间】:2017-04-11 03:34:55
【问题描述】:

我在过去两天遇到问题。我正在尝试在没有 reportviewer 的情况下以 pdf 格式查看 rdlc 报告。我使用以下代码将 rdlc 导出为 pdf...

public string Export(LocalReport rpt, string filePath)
    {
        string ack = "";
        try
        {                
            Warning[] warnings;
            string[] streamids;
            string mimeType;
            string encoding;
            string extension;

            byte[] bytes = rpt.Render("PDF", null, out mimeType, out encoding, out extension, out streamids, out warnings);
            using (FileStream stream = File.OpenWrite(filePath))
            {
                stream.Write(bytes, 0, bytes.Length);
            }
            return ack;
        }
        catch(Exception ex)
        {
            ack = ex.InnerException.Message;
            return ack;
        }           
    }

导出到User->AppData->Temp的pdf文件

            string filePath = System.IO.Path.GetTempFileName();
            string ack = Export(rpt, Server.MapPath("~/Reports/Mymun_Lab/ComparisonStudy.rdlc"));                
            System.Diagnostics.Process.Start(filePath);

我想将此 pdf 文件渲染到客户端 PC。

此代码在我的本地机器上运行良好。但是当我将它发布到 IIS 服务器并运行时,尝试将导出的 pdf 文件发送到客户端 PC 不成功。我该如何解决这个问题。谁能帮帮我。

提前致谢...

【问题讨论】:

    标签: c# asp.net-mvc asp.net-mvc-4 pdf-generation rdlc


    【解决方案1】:

    最后,我找到了一种在 asp.net MVC 中以 pdf 格式查看 RDLC 报告的解决方案。解决方法如下......

    public FileResult ViewReport()
        {            
            string RptPath = Server.MapPath("~/Reports/Mymun_Lab/ComparisonStudy.rdlc");                  
            Microsoft.Reporting.WebForms.LocalReport rpt = new Microsoft.Reporting.WebForms.LocalReport(); 
    
            /* Bind Here Report Data Set */
    
            rpt.ReportPath = RptPath;
            string filePath = System.IO.Path.GetTempFileName();               
            Export(rpt, filePath);
            //CLOSE REPORT OBJECT           
            rpt.Dispose();
            return File(filePath, "application/pdf");
        } 
    

    我使用以下方法从 RDLC 生成 pdf....

    public string Export(LocalReport rpt, string filePath)
    {
        string ack = "";
        try
        {                
            Warning[] warnings;
            string[] streamids;
            string mimeType;
            string encoding;
            string extension;
    
            byte[] bytes = rpt.Render("PDF", null, out mimeType, out encoding, out extension, out streamids, out warnings);
            using (FileStream stream = File.OpenWrite(filePath))
            {
                stream.Write(bytes, 0, bytes.Length);
            }
            return ack;
        }
        catch(Exception ex)
        {
            ack = ex.InnerException.Message;
            return ack;
        }           
    }
    

    【讨论】:

    • 我收到返回文件错误(filePath, "application/pdf")
    • 在哪里添加 Export() 方法?直接在控制器或单独的类?您是否在项目参考中添加了正确版本的 Microsoft.ReportViewer.WebForms.dll? @ElbertJohnFelipe
    【解决方案2】:
    [HttpPost]
    public async Task<FileResult> DownloadReport(string id, string firstName, string lastName)
    {
        var result = await accountRepository.GetUserLogInDetailsByID(id);
    
        //string RptPath = Server.MapPath("~/RDLC/rptUserLogInDetails.rdlc");
        string RptPath = Server.MapPath("~/RDLC/rptUserLogInDetails.rdlc");
    
        ReportViewer rv = new ReportViewer();
    
        ReportDataSource rds = new ReportDataSource();
        rds.Name = "DataSet1";
        rds.Value = result;
    
        string companyName = WebConfigurationManager.AppSettings["CompanyName"];
    
        ReportParameter[] parameters = new ReportParameter[2];
        parameters[0] = new ReportParameter("username", firstName + " " + lastName);
        parameters[1] = new ReportParameter("companyName", companyName);
    
        //ReportViewer rv = new Microsoft.Reporting.WebForms.ReportViewer();
        rv.ProcessingMode = ProcessingMode.Local;
        rv.LocalReport.ReportPath = RptPath;
    
        // Add the new report datasource to the report.
        rv.LocalReport.DataSources.Add(rds);
        rv.LocalReport.EnableHyperlinks = true;
    
        rv.LocalReport.SetParameters(parameters);
    
        rv.LocalReport.Refresh();
    
        byte[] streamBytes = null;
        string mimeType = "";
        string encoding = "";
        string filenameExtension = "";
        string[] streamids = null;
        Warning[] warnings = null;
    
        streamBytes = rv.LocalReport.Render("PDF", null, out mimeType, out encoding, out filenameExtension, out streamids, out warnings);
    
        string fileName = "LogInDetails-" + firstName + lastName + ".pdf";
    
        // This will download the pdf file
        //return File(streamBytes, mimeType, fileName);
    
        //This will open directly the pdf file
        return File(streamBytes, "application/pdf");
    }
    

    【讨论】:

      猜你喜欢
      • 2011-12-20
      • 1970-01-01
      • 1970-01-01
      • 2017-08-06
      • 1970-01-01
      • 2013-09-12
      • 1970-01-01
      • 2023-03-18
      • 2012-01-24
      相关资源
      最近更新 更多