Read From Excel Files
The example below explains how to read information from a row or a column, inside an .xlsx file. It presents activities such as Read Row and Read Column. You can find these activities in the UiPath.Excel.Activities package.
This is how the automation process can be built:
-
Open Studio and create a new Process named by default Main.
-
Drag a Sequence container in the Workflow Designer.
- Create a variable named
totalColumnIndexof type Int32 and with the Default value of0.
- Create a variable named
-
Place an Excel Application Scope inside the body of the sequence.
- Add the following path
"read_col_row_example.xls"in the WorkbookPath field. - In the Properties panel, select the check boxes for the AutoSave, CreateNewFile and Visible options. The robot is now allowed to create a new Excel workbook, to automatically save all the changes made to it and to open the Excel file in the foreground while performing actions on it.
- Add the following path
-
The Excel Application Scope includes a Do sequence. Select the sequence.
- Create the following variables:
Variable Name Variable Type Default Value firstRowValueIEnumerable<object> N/A totalColumnValuesIEnumerable<object> N/A columnLetterChar N/A -
Add a Read Row activity inside the Do sequence.
- In the Properties panel, add the name of the sheet
"SalesOrders"in the SheetName field. Add the value"A1"in the StartingCell field and the variablefirstRowValuesin the Result field.
- In the Properties panel, add the name of the sheet
-
Drag an Assign activity below the Read Row activity.
- Add the variable
totalColumnIndexin the To column and the valuearray.IndexOf(firstRowValues.ToArray.Cast(of string).ToArray, "Total")in the Value field.
- Add the variable
-
Add another Assign activity right below the previous one.
- Insert the variable
columnLetterin the To field and the valueconvert.ToChar(totalColumnIndex + 65)in the Value field.
- Insert the variable
-
Place an Excel Read Column activity under the Assign activity.
- In the Properties panel, add the name of the sheet
"SalesOrders"in the SheetName field, add the valuecolumnLetter + "2"in the StartingCell field and the variabletotalColumnValuesin the Result field, as output.
- In the Properties panel, add the name of the sheet
-
Add a For Each activity underneath the Read Column activity.
- Insert the variable
totalColumnValuesin the Values field.
- Insert the variable
-
Drag a Write Line activity in the body of the For Each activity.
- Add the value
item.ToStringin the Text field.
- Add the value
-
Run the process. The robot reads the first row of the excel file, which is considered the header, computes the index of the Total column, reads it, and logs the values in the console.