Exporting a report created in code

Posted by: sstoecker on 23 June 2020, 11:03 pm EST

    • Post Options:
    • Link

    Posted 23 June 2020, 11:03 pm EST

    I have created a report in C# code that displays how I want it on screen, but when I attempt to export it to PDF (or Excel or Word), nothing displays.

    This is the class that passes the report through the system.

        public class ExportSectionReport : Message<IReport>
        {
            public ExportSectionReport(IReport value, string path)
                : base(value)
            {
                Path = path;
            }
    
            public string Path { get; }
    
            [b]public IReport Report => Value;[/b]
        }
    

    IReport is a custom-defined defined class, not one created by GrapeCity. The SectionReport property is of type GrapeCity.ActiveReports.SectionReport:

        public interface IReport
        {
            DataTable Data { get; }
    
            [b]SectionReport Section { get; }[/b]
        }
    

    All of our reports are Section reports, and most are created with the designer and have a visual interface created at design time. The way we generate the export is:

    private IMessage Handle(ExportSectionReport msg) {
                var pdfExport = new PdfExport
                {
                    ConvertMetaToPng = false,
                    ImageQuality = ImageQuality.Highest
                };
    
                msg.Report.Section.Run(true);
                pdfExport.Export(msg.Report.Section.Document, msg.Path);
    }
    

    The report I am passing through the system inherits from SectionReport:

    public partial class CustomerReport : SectionReport
    

    For the report generated in code, this doesn’t work. The export completes, but the PDF is empty. I suspect this is because there is no visual interface. Looking at the msg.Report.Section.Document.Content property after the msg.Report.Section.Run has executed, the Content for a report designed in the designer is far larger than the one designed in code. I am guessing that is because the data is merged with the visual controls to create the export. The data is associated with Section.DataSource is correct in both version, so the data appears to be assigned.

    So, is there a different way to export a code-generated report? Perhaps an overload of PdfExport.Export()?

  • Posted 24 June 2020, 4:40 pm EST

    Hello,

    Can you check the RDF file in the viewer(C:\Program Files (x86)\GrapeCity\ActiveReports 14\Tools\GrapeCity.ActiveReports.Viewer.exe) after saving the section report into RDF format using the following line in “Handle” method:

    
    msg.Report.Section.Run(true);
    msg.Report.Section.Document.Save("Text.rdf");
    
    

    If viewer shows the blank out then, it must be the problem in your code as section report has no content. If it shows data, then could you please share the stripped down sample so that I can replicate the issue at my end.

    This will be very helpful for us.

    Thanks,

    Mohit

  • Posted 25 June 2020, 9:44 pm EST

    I was able to export the code-generated report successfully. First, I created a SectionDocument variable:

    private GrapeCity.ActiveReports.Document.SectionDocument sectionDocument;
    

    I then create a SectionReport:

    GrapeCity.ActiveReports.SectionReport ar = default(GrapeCity.ActiveReports.SectionReport);
    

    After populating it with data and creating the fields and such, I load it in the viewer and store it in the variable:

    ReportViewerPane.LoadDocument(ar);

    sectionDocument = ar.Document;

    When the user clicks the export button (the Excel button, in this instance), I export the SectionDocument from the private variable I defined:

    GrapeCity.ActiveReports.Export.Excel.Section.XlsExport XlsExport1 = new GrapeCity.ActiveReports.Export.Excel.Section.XlsExport();

    XlsExport1.FileFormat = GrapeCity.ActiveReports.Export.Excel.Section.FileFormat.Xlsx;

    XlsExport1.Export(sectionDocument, selectedFileName);

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels