在Asp.net MVC中查看RDLC报告为pdf

时间:2016-11-27 08:02:15

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

我在过去两天遇到问题。我试图在没有reportviewer的情况下将rdlc报告视为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;
        }           
    }

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

            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时不成功。我该如何解决这个问题。任何人都可以帮助我。

提前致谢...

2 个答案:

答案 0 :(得分:4)

最后,我在asp.net MVC中找到了一个关于将RDLC报告视为pdf的解决方案。解决方案正在关注....

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;
    }           
}

答案 1 :(得分:0)

[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");
}