打开 Excel 文件

打开一个 Excel 文件。

命令可用性: 本地 IBM RPA SaaS 和 IBM RPA

Description

打开一个 Excel 文件。 此命令还可以创建新的 Excel 文件。 要执行此操作,必须在 File path 参数中引用不存在的文件路径。 使用 Set Value in Excel (excelSet) 或 Insert Data Table into Excel File (excelSetTable) 命令来设置文件的值, 并使用 关闭 Excel (excelClose) 命令来保存和关闭文件。

脚本语法

IBM RPA 的专有脚本语言的语法与其他编程语言类似。 该脚本语法在脚本文件中定义命令的语法。 您可以在 IBM RPA Studio的 脚本 方式下使用此语法。

excelOpen --file(String) [--passwordtopen(String)] [--passwordtomodify(String)] [--usetemplate(Boolean)] --template(String) [--savechanges(Boolean)] [--notcalculate(Boolean)] (Boolean)=success (String)=reason (Excel)=value

依赖关系

  • 此命令仅支持具有 .xlsx 文件扩展名的文件。 如果您有 .xls 文件,请使用 将 Excel 从 XLS 转换为 XLSX (excelToXlsx) 命令在打开该文件之前对其进行转换。
  • 使用 关闭 Excel 文件 (excelclose) 命令来关闭使用 打开 Excel 文件 命令打开的 Excel 文件。

输入参数

下表显示了此命令中提供的输入参数的列表。 在表中,您可以看到在 IBM RPA Studio的脚本方式及其 Designer 方式等效标签中工作时的参数名称。

设计器方式标签 脚本方式名称 必需的 接受的变量类型 Description
文件路径 file Required Text Excel 文件路径。
读取密码 passwordtopen Optional Text 仅允许读取文件内容的密码。
修改密码 passwordtomodify Optional Text 允许修改文件内容的密码。
使用模板 usetemplate Optional Boolean 用于在打开 Excel 文件时使用现有模板的选项。
模板 template Required when the Use template parameter is enabled Text 为 excel 文件接口设置新模板。
保存更改 savechanges Optional Boolean 根据在运行时发生的每次更改保存 Excel 文件。
不计算 notcalculate Optional Boolean 在保存更改之前,请勿计算 Excel 文件中包含的公式。

输出参数

设计器方式标签 脚本方式名称 接受的变量类型 Description
成功 成功 Boolean 如果文件已打开,那么返回 True ,否则返回 False
原因 reason Text 说明在 Success 参数中获取的返回的理由。
Excel 实例 Excel 返回包含所打开 Excel 文件的变量。

示例

示例 1: 以下代码示例演示如何打开 Excel 文件。

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.

重要信息: 要运行样本脚本,请创建 excel 文件。

示例 2: 以下示例演示了该命令如何创建新文件,同时添加内容并保存文件。

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.