The AL developer preview is still evolving with frequent updates. Follow us on the Dynamics NAV Team Blog for the announcements.
SAVEASEXCEL Method (Report)
Saves a report on the Dynamics 365 for Financials service as a Microsoft Excel (.xls) workbook.
[Ok :=] REPORT.SAVEASEXCEL(Number, FileName[, Record]) [Ok :=] ReportVariable.SAVEASEXCEL(FileName)
The ID of the report that you want to run.
If the report you specify does not exist, then a run-time error occurs.
The path and the name of the file that you want to save the report as. The path must exist, the file must not be being used, and the Dynamics 365 for Financials service process must have permission to write to the file. Otherwise, you will get errors.
Specifies which record to use in the report. Any filters that have been applied to the record that you specify will be used.
The report that you want to run.
Property Value/Return Value
If you omit this optional return value, then run-time errors can occur. If you include a return value, then it is assumed that you will handle any errors.
true if the report was saved; otherwise, false.
You can use the SAVEASEXCEL method on the global REPORT object and on Report variables. If, at design time, you do not know the specific report that you want to run, then use the global REPORT object and specify the report number in the Number parameter. If you do know which report you want to run, then create a Report variable, set the Subtype of the variable to a specific report, and use this variable when you call the SAVEASEXCEL method.
When you call the SAVEASEXCEL method, the report is generated and saved to "FileName." The request page is not shown.
If the destination folder that you specify in FileName does not exist, then you get the following error:
The specified path is invalid.
If the file that you specify in FileName is being used, then you get the following error:
An I/O exception occurred during the operation.
If the Dynamics 365 for Financials service process does not have permission to write to the file that you specify in FileName, then you get the following error:
Either the caller does not have the required permission or the specified path is read-only.
This example shows how to use the SAVEASEXCEL method to save the Excel workbook to the Dynamics 365 for Financials service and then download the file to a computer that is running the Dynamics 365 application. It requires that you create the following variables.
// Specify that TempFile is opened as a binary file. TempFile.TEXTMODE(FALSE); // Specify that you can write to TempFile. TempFile.WRITEMODE(TRUE); Name := 'C:\Temp\TempReport.xls'; // Create and open TempFile. TempFile.CREATE(Name); // Close TempFile so that the SAVEASEXCEL method can write to it. TempFile.CLOSE; REPORT.SAVEASEXCEL(406,Name); TempFile.OPEN(Name); TempFile.CREATEINSTREAM(NewStream); ToFile := 'Report.xls'; // Transfer the content from the temporary file on the // server to a file on the client. ReturnValue := DOWNLOADFROMSTREAM( NewStream, 'Save file to client', '', 'Excel File *.xls| *.xls', ToFile); // Close the temporary file. TempFile.CLOSE();
You can create an action on a page and set the action to run this code. When you run the action, the Export File dialog box opens. Choose Save to save the file to the client.