Dynamics 365 Business Central: How to save the report as a PDF, Excel, Word, HTML, or XML file (Report.RunRequestPage Method and Report.SaveAs Method)

Dynamics 365 Business Central

Hi, Readers.
As you know, we can view the dataset from the preview page in NAV, similar to the page inspector. For the development of complex reports it is very important to be able to analyze the data before print.

But unfortunately, in Business Central, this feature is not available so far.

So in this post, I want to discuss how to save the report as a PDF, Excel, Word, HTML, or XML file in AL. This may be helpful for analyzing the data.

The methods to be utilized this time are mainly Report.RunRequestPage Method and Report.SaveAs Method.

Report.RunRequestPage Method:
Runs the request page for a report without running the report. Returns an XML string that contains the request page parameters that are entered on the request page.

ReportParameters := Report.RunRequestPage([PageParameters: String])

This method opens the request page for the specified report, where the user can provide parameters for the report. When the user closes the request page by choosing the OK button, a string that contains the parameter values that were set by the user is returned. When the user chooses the Cancel button, an empty string will be returned. The returned parameter string can be picked up by calling one of the following methods:

Report.SaveAs Method:
Runs a specific report without a request page and saves the report as a PDF, Excel, Word, HTML, or XML file.

[Ok := ] Report.SaveAs(Number: Integer, Parameters: String, Format: ReportFormat, var OutStream: OutStream [, RecordRef: RecordRef])

You typically use this method together with the RUNREQUESTPAGE Method method. The RUNREQUESTPAGE method runs a report request page without actually running the report, but instead, returns the parameters that are set on the request page as a string. You can then call the SAVEAS method to get the parameter string and save the report to a file of the specified format.

The scenario:
1. Add a new action Export Report on the Report Layout Selection page
2. User can select the export format of the file after choosing the action
3. Export file name format is ReportID_ReportName_CurrentDateTime

OK, let’s start.

1. Add a new action Export Report on the Report Layout Selection page

2. User can select the export format of the file after choosing the action

3. Export the report and file name format is ReportID_ReportName_CurrentDateTime.type

Test Video 01: RDLC report layout
Standard Sales – Invoice (1306, Report Request)

Note: Html type is only supported when a report uses a Word report layout when it is run.

Test Video 02: Word report layout
Standard Sales – Credit Memo (1307, Report Request)

Note: Excel type is only supported when a report uses a RDLC report layout when it is run.

Source Code:

pageextension 80100 ReportLayoutSelectionEXT extends "Report Layout Selection"
{
    actions
    {
        addbefore(RunReport)
        {
            action(ExportReport)
            {
                Caption = 'Export Report';
                Image = Export;
                PromotedCategory = Report;
                Promoted = true;
                PromotedIsBig = true;
                ApplicationArea = All;

                trigger OnAction()
                var
                    ReportParameters: text;
                    TempBlob: Codeunit "Temp Blob";
                    FileManagement: Codeunit "File Management";
                    OStream: OutStream;
                    SelectedExportType: Integer;
                    ExportType: Label 'PDF,Excel,Word,HTML,XML';
                    StrMenuMsg: Label 'Please choose one of the export types:';
                begin
                    Clear(ReportParameters);
                    Clear(OStream);
                    Clear(SelectedExportType);
                    SelectedExportType := StrMenu(ExportType, 2, StrMenuMsg); //Defualt format is Excel
                    if SelectedExportType = 0 then
                        exit;
                    ReportParameters := Report.RunRequestPage(Rec."Report ID");
                    TempBlob.CreateOutStream(OStream);
                    case SelectedExportType of
                        1: //Pdf
                            begin
                                Report.SaveAs(Rec."Report ID", ReportParameters, ReportFormat::Pdf, OStream);
                                FileManagement.BLOBExport(TempBlob, Format(Rec."Report ID") + '_' + Rec."Report Name" + '_' + Format(CURRENTDATETIME, 0, '<Day,2><Month,2><Year4><Hours24><Minutes,2><Seconds,2>') + '.pdf', true);
                            end;

                        2: //Excel
                            begin
                                Report.SaveAs(Rec."Report ID", ReportParameters, ReportFormat::Excel, OStream);
                                FileManagement.BLOBExport(TempBlob, Format(Rec."Report ID") + '_' + Rec."Report Name" + '_' + Format(CURRENTDATETIME, 0, '<Day,2><Month,2><Year4><Hours24><Minutes,2><Seconds,2>') + '.xlsx', true);
                            end;
                        3: //Word
                            begin
                                Report.SaveAs(Rec."Report ID", ReportParameters, ReportFormat::Word, OStream);
                                FileManagement.BLOBExport(TempBlob, Format(Rec."Report ID") + '_' + Rec."Report Name" + '_' + Format(CURRENTDATETIME, 0, '<Day,2><Month,2><Year4><Hours24><Minutes,2><Seconds,2>') + '.docx', true);
                            end;
                        4: //Html
                            begin
                                Report.SaveAs(Rec."Report ID", ReportParameters, ReportFormat::Html, OStream);
                                FileManagement.BLOBExport(TempBlob, Format(Rec."Report ID") + '_' + Rec."Report Name" + '_' + Format(CURRENTDATETIME, 0, '<Day,2><Month,2><Year4><Hours24><Minutes,2><Seconds,2>') + '.html', true);
                            end;
                        5: //Xml
                            begin
                                Report.SaveAs(Rec."Report ID", ReportParameters, ReportFormat::Xml, OStream);
                                FileManagement.BLOBExport(TempBlob, Format(Rec."Report ID") + '_' + Rec."Report Name" + '_' + Format(CURRENTDATETIME, 0, '<Day,2><Month,2><Year4><Hours24><Minutes,2><Seconds,2>') + '.xml', true);
                            end;
                    end;
                end;
            }
        }
    }
}

END

Hope this will help.

Thanks for your reading.

ZHU

コメント

タイトルとURLをコピーしました