Open Excel File
Opens an Excel file.
Command availability: IBM RPA SaaS and IBM RPA on premises
Description
Opens an Excel file. This command can also create a new Excel file. To do it, you must reference a non-existent file path in the File path parameter, use the Set Value in Excel (excelSet)
or Insert Data Table into Excel File (excelSetTable) command to set a value to the file, and use the Close Excel (excelClose) command
to save and close the file.
Script syntax
IBM RPA's proprietary script language has a syntax similar to other programming languages. The script syntax defines the command's syntax in the script file. You can work with this syntax in IBM RPA Studio's Script mode.
excelOpen --file(String) [--passwordtopen(String)] [--passwordtomodify(String)] [--usetemplate(Boolean)] --template(String) [--savechanges(Boolean)] [--notcalculate(Boolean)] (Boolean)=success (String)=reason (Excel)=value
Dependencies
- This command supports only files with the
.xlsxfile extension. If you have an.xlsfile, use the Convert Excel from XLS to XLSX (excelToXlsx) command to convert the file before opening it. - Use the Close Excel File (
excelclose) command to close the Excel files that you opened using the Open Excel File command.
Input parameters
The following table displays the list of input parameters available in this command. In the table, you can see the parameter name when working in IBM RPA Studio's Script mode and its Designer mode equivalent label.
| Designer mode label | Script mode name | Required | Accepted variable types | Description |
|---|---|---|---|---|
| File path | file |
Required |
Text |
Excel file path. |
| Reading password | passwordtopen |
Optional |
Text |
Password that allows only reading the contents of the file. |
| Modification password | passwordtomodify |
Optional |
Text |
Password that allows you to modify the contents of the file. |
| Use template | usetemplate |
Optional |
Boolean |
Option for using an existing template when opening the Excel file. |
| Template | template |
Required when the Use template parameter is enabled |
Text |
Sets a new template for the excel file interface. |
| Save changes | savechanges |
Optional |
Boolean |
Saves the Excel file according to each change that occurs at run time. |
| Not calculate | notcalculate |
Optional |
Boolean |
Do not calculate formulas that are contained in the Excel file before saving changes. |
Output parameters
| Designer mode label | Script mode name | Accepted variable types | Description |
|---|---|---|---|
| Success | success | Boolean |
Returns True if the file was opened, or False otherwise. |
| Reason | reason | Text |
Reason that justifies the return obtained in Success parameter. |
| Excel instance | value | Excel |
Returns a variable with the Excel file used. |
Example
Example 1: The following code example demonstrates how to open an Excel file.
defVar --name excelFile --type Excel
excelOpen --file "samplefile" excelFile=value
// Result: Stores the opened excel file in the excelFile variable to use it in the script.
❕ Important: To run the sample script, create the excel file.
Example 2: The following example demonstrates how the command creates a new file, while adding content and saving the file.
defVar --name excelFile --type Excel
defVar --name desktopPath --type String
defVar --name sampleFile --type String
defVar --name dataTableSample --type DataTable
// Gets the path to the Desktop folder.
getSpecialFolder --folder "Desktop" desktopPath=value
// Sets the path to the sample Excel file.
setVar --name "${sampleFile}" --value "${desktopPath}\\sample_file.xlsx "
// Adds a column called "Fruit" to the sample data table.
addColumn --dataTable ${dataTableSample} --columnname Fruit --type String
// Adds a row with the "Apple" value in the Fruit column.
addRow --valuesmapping "Fruit=Apple" --dataTable ${dataTableSample}
// Opens the Excel file based on the path in the ${sampleFile} variable. Itcreates a file called "sample_file", if it doens't exist in the file path provided.
excelOpen --file "${sampleFile}" excelFile=value
// Sets the value in the ${dataTableSample} variable to the Excel file.
excelSetTable --dataTable ${dataTableSample} --headers --file ${excelFile} --getfirstsheet --row 1 --column 1
// Closes and saves the Excel file.
excelClose --file ${excelFile} --save
// Check if the file was successfully created in your Desktop area.