Report.SaveAsExcel(Text) Method
Version: Available or changed with runtime version 1.0.
Saves a report on the computer that is running the server as a Microsoft Excel (.xls) workbook.
Note
This method is supported only in Business Central on-premises.
Syntax
[Ok := ] Report.SaveAsExcel(FileName: Text)
Parameters
Report
Type: Report
An instance of the Report data type.
FileName
Type: Text
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 server process must have permission to write to the file. Otherwise, you will get errors.
Return Value
[Optional] Ok
Type: Boolean
true if the operation was successful; otherwise false. If you omit this optional return value and the operation does not execute successfully, a runtime error will occur.
Remarks
You can use the SaveAsExcel method on the global Report object and on instances of a Report variable. If, at design time, you do not know the specific report that you want to run, then use the static version of the method: Report.SaveAsExcel(Integer, Text [, var Record]).
When you call the SaveAsExcel method, the report is generated and saved to the file specified in "FileName." The request page is not shown.
Note
The FileName parameter specifies a location on the computer that is running Business Central. If you call this method from a client, such as from an action on a page, then use the File.Download method to download the .pdf file from the computer that is running the Business Central server to the computer that is running the client.
Error conditions
The method can fail in the following three ways:
- If the destination folder specified in FileName does not exist,
- If the file specified in FileName is being used, or
- If the Business Central server process does not have permission to write to the file specified in FileName.
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 Business Central server 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.
Example
This example shows how to use the SaveAsExcel method to generate the report in the Business Central server and then download the Excel workbook file to the server.
var
MyReport: Report MyReport
TempFile: File;
FileNameAndPath: Text[250];
NewStream: InStream;
ToFile: Text[250];
ReturnValue: Boolean;
begin
// Specify that TempFile is opened as a binary file.
TempFile.TextMode(False);
// Specify that you can write to TempFile.
TempFile.WriteMode(True);
FileNameAndPath := 'C:\Temp\TempReport.xlsx';
// Create and open TempFile.
TempFile.Create(FileNameAndPath);
// Close TempFile so that the SaveAsExcel method can write to it.
TempFile.Close;
MyReport.SaveAsExcel(FileNameAndPath);
TempFile.Open(FileNameAndPath);
TempFile.CreateInStream(NewStream);
ToFile := 'MyReport.xlsx';
// 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 *.xlsx| *.xlsx',
ToFile
);
// Close the temporary file.
TempFile.Close();
end;
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.