Search code examples
c#winformsdevexpressxtrareport

how to save devexpress xtra report in database or file and load it from database or file


I am developing a product application. It is windows form application using devexpress control. This application is a billing application.

My clients wants to design custom invoice format. If I add reports in my visual studio project then I have to deploy all reports to all clients. I want to deploy the report that is developed specific for the client. To do this I want a way that I can save Xtrareport as a file or in database. So that I can load it on runtime dynamically.

Is there anyway to do this ?

Environment : C# 4.5 WinForms DevExpress 14.2.4.0


Solution

  • Yes you can save a report to a file or into a db using System.IO; using DevExpress.XtraReports.UI; // ...

    // Save a report to a file. 
    private string StoreReportToFile(XtraReport report){
        string path = "C:\\MyReport.repx";
        report.SaveLayout(path);
        return path;
    }
    
    // Save a report to a stream. 
    private MemoryStream StoreReportToStream(XtraReport report){
        MemoryStream stream = new MemoryStream();
        report.SaveLayout(stream);
        return stream;
    }
    

    And load it

    using System.IO;
    using DevExpress.XtraReports.UI;
    // ... 
    
    // Load a report from a file. 
    private void LoadReportFromFile(XtraReport report, string filePath){
        if (File.Exists(filePath)) {
            report.LoadLayout(filePath);
        } 
        else {
            Console.WriteLine("The source file does not exist.");
        }
    }
    
    // Load a report from a stream. 
    private void LoadReportFromStream(XtraReport report, MemoryStream stream){
        report.LoadLayout(stream);
    }
    
    // Create a report from a file. 
    private XtraReport CreateReportFromFile(string filePath){
        XtraReport report = XtraReport.FromFile(filePath, true);
        return report;
    }
    
    // Create a report from a stream. 
    private XtraReport CreateReportFromStream(MemoryStream stream){
        XtraReport report = XtraReport.FromStream(stream, true);
        return report;
    }
    

    You can find more info here: https://documentation.devexpress.com/#XtraReports/CustomDocument2592