How to generate a standalone report from an ASP.NET MVC web application?

5 posts, 1 answers
  1. Ian
    Ian avatar
    11 posts
    Member since:
    Apr 2014

    Posted 28 Jul 2014 Link to this post

    Hello, I am new to Telerik. I want to have a button in my ASP.NET MVC web application that when clicked:

    1. executes a .trdx standalone Telerik file
    2. exports the output to a Word file

    I don't know how to get started with this, what tutorial might help explain this? What sort of methods might be available? Here is my pseudo-code of what I would like to do:

    string filePath = Server.MapPath("~/reportFolder/reportFile.trdx");
    // I made this stuff up:
    Telerik.StandaloneReport myReport = new Telerik.StandaloneReport(filePath);
    myReport.executeAndPromptTheUserToSaveAs();

    I have the .trdx file linked to my database, and so it is already correctly processing the report I want. Keep in mind that I don't want to embed the report in the web application in any way, I just want to "Save as..." the output of the .trdx report file when clicking a button in the application.

  2. Stef
    Admin
    Stef avatar
    3050 posts

    Posted 30 Jul 2014 Link to this post

    Hi Ian,

    If you do not intend to display the report in a report viewer, you can use custom UI to post to your controller's action in which the report is exported programmatically and the file result is sent back to the client.

    You will need to copy the connection strings used in the report in the start application's configuration file. Also if you use the Open XML formats (DOCX, XLSX) you will need to add references to Telerik.Reporting.OpenXmlRendering.dll and Open XML SDK 2.0 for Microsoft Office (DocumentFormat.OpenXml.dll v.2.0.5022.0 or above).


    I hope the above information helps you.

    Regards,
    Stef
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  3. DevCraft banner
  4. Ian
    Ian avatar
    11 posts
    Member since:
    Apr 2014

    Posted 05 Aug 2014 in reply to Stef Link to this post

    Thanks very much Stef for the help!

    So I now have the .dll assemblies you mentioned, and have successfully compiled the sample code you mentioned found at http://www.telerik.com/help/reporting/programmatic-exporting-report.html#cbc_1 . I'm using the "DOCX" format instead of "PDF" that the example uses.

    However, I don't know how to correctly use this line from the example code:
    // reportName is the Assembly Qualified Name of the report
    typeReportSource.TypeName = reportName;

    This is my attempt, which yields the error ArgumentException "Invalid report type":
    typeReportSource.TypeName = Server.MapPath("~/Telerik/DashboardReport.trdx");

    The above-mentioned exception occurs on this line from the example code:
    Telerik.Reporting.Processing.RenderingResult result = reportProcessor.RenderReport("DOCX", typeReportSource, deviceInfo);


    So how do I correctly set this TypeName property, and how do I link to my .trdx file?
  5. Answer
    Stef
    Admin
    Stef avatar
    3050 posts

    Posted 08 Aug 2014 Link to this post

    Hello,

    The AssemblyQualifiedName of an object is a string structured as follows:
    <full_namespace>.<type_name>, <assembly_name>
    It can be used with the reporting TypeReportSource to specify a report created with the integrated in Visual Studio Report Designer. If the report is in an external DLL, the DLL must be referenced in the viewer's project, where both projects must use the same Telerik Reporting version.Using reflection we create internally an instance of the report type through its default constructor.

    If the reports are saved in TRDX files, you can use a UriReportSource and set its Uri property to the file path.

    For more details about report source types and their usage, please check the Report Sources help article.


    Let us know if you have any further questions.

    Regards,
    Stef
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  6. Ian
    Ian avatar
    11 posts
    Member since:
    Apr 2014

    Posted 08 Aug 2014 in reply to Stef Link to this post

    Thanks again Stef! UriReportSource did the trick, as you are saying. I am calling the below C# method in my MVC controller from a Razor Html.ActionLink in my view:

    public ActionResult ExportReport()
    {
        Telerik.Reporting.Processing.ReportProcessor reportProcessor =
            new Telerik.Reporting.Processing.ReportProcessor();
     
        System.Collections.Hashtable deviceInfo =
            new System.Collections.Hashtable();
        deviceInfo.Add("DocumentAuthor", User.Identity.Name);
     
        Telerik.Reporting.UriReportSource uriReportSource =
            new Telerik.Reporting.UriReportSource();
        uriReportSource.Uri = Server.MapPath("~/MyFolder/MyReport.trdx");
     
        Telerik.Reporting.Processing.RenderingResult result =
            reportProcessor.RenderReport("DOCX", uriReportSource, deviceInfo);
     
        string fileName = result.DocumentName + "." + result.Extension;
        string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
        string filePath = System.IO.Path.Combine(path, fileName);
     
        using (System.IO.FileStream fs = new System.IO.FileStream(filePath, System.IO.FileMode.Create))
        {
            fs.Write(result.DocumentBytes, 0, result.DocumentBytes.Length);
        }
     
        return RedirectToAction("Index");
    }
Back to Top
DevCraft banner