Manage Databases in Excel
The example below explains how to read a database, create a new database and filter it based on the results. It presents activities such as Read Range, Create Table, Insert Column, Build Data Table, and Add Data Row. 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.
Make sure to have the .xlsx file inside the project folder. You can use your own .xlsx file or download the example we have provided at the bottom of this page. The .xlsx file is used for retrieving and processing data to generate a new database.
-
Drag a Sequence container in the Workflow Designer.
- Create the following variables:
Variable Name Variable Type Default Value excelFileString "Exam results.xlsx"resultsDtSystem.Data.DataTable N/A studentGradesDtSystem.Data.DataTable N/A passedStudentsInt32 0 failedStudentsInt32 0 statisticsDtSystem.Data.DataTable N/A -
Drag an Excel Application Scope inside the sequence.
- In the Properties panel, add the value
excelFilein the WorkbookPath field. - 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.
- In the Properties panel, add the value
-
Place an Excel Read Range activity inside the Do sequence of the Excel Application Scope activity.
- In the Properties panel, add the value
"Sheet1"in the SheetName field and the variablestudentGradesDtin the DataTable output field. - Select the check box for the AddHeaders options for considering the first row as header.
- In the Properties panel, add the value
-
Drag a Create Table activity under the Read Range activity.
- In the Properties panel, add the value
"Sheet1!$A$1:$C$11"in the Range field, the name"Grades"in the TableName field and the name"Sheet1"in the SheetName input field.
- In the Properties panel, add the value
-
Add an Insert Column activity below the Create Table activity.
- Configure the following settings in the Properties panel: add the name
"Result"in the ColumnName field, add the name"Sheet1"in the SheetName field and the name"Grades"in the TableName field.
- Configure the following settings in the Properties panel: add the name
-
Place a Build Data Table activity under the Insert Column activity.
- Add the variable
resultsDtin the DataTable output field. - Click on the DataTable button, inside the body of the Build Data Table activity.
- Click on the Edit Column button and add the name
Resultin the ColumnName field. - Set the Data Type as String.
- Select the Allow Null check box to allow the robot to take into consideration the null values.
- Add the value
100in the Max Length field. - Select the OK option for closing the window.
- Select the OK button again to close the window.
- Add the variable
-
Drag a For Each Row activity below the Build Data Table activity.
- In the Properties panel, add the variable
studentGradesDtin the DataTable field.
- In the Properties panel, add the variable
-
Add an If condition inside the body of the For Each Row activity.
- Insert the value
cint(row("Grade")) < 5in the Condition field. This formula analyses the data available in the rowGradefrom the.xlsxfile and decides if the value is smaller or bigger than5.
- Insert the value
-
Drag two new Sequences, one in the Then and one in the Else fields.
- Name the sequence from the Then area
Failedand the sequence from the Else area,Passed.
- Name the sequence from the Then area
-
Place an Add Data Row activity inside the Failed sequence.
- In the Properties panel, add the the condition
{ "Failed" }in the ArrayRow field and the variableresultsDtin the DataTable field.
- In the Properties panel, add the the condition
-
Place an Assign activity below the Add Data Row activity.
- Add the variable
failedStudentsin the To field and the conditionfailedStudents + 1in the Value field.
- Add the variable
-
Place an Add Data Row activity inside the Passed sequence.
- In the Properties panel, add the the condition
{ "Passed" }in the ArrayRow field and the variableresultsDtin the DataTable field.
- In the Properties panel, add the the condition
-
Place an Assign activity below the Add Data Row activity.
- Add the variable
passedStudentsin the To field and the conditionpassedStudents + 1in the Value field.
- Add the variable
-
Place a Build Data Table activity under the For Each Row activity.
- In the Properties panel, add the variable
statisticsDtin the DataTable output field. - Click on the DataTable button, inside the body of the Build Data Table activity.
- Click on the Edit Column button from the left side and add the name
Failedin the ColumnName field. - Set the Data Type as Int32.
- Select the check box for the Allow Null option for allowing the robot to take into consideration the null values.
- Select the OK option for closing the window.
- Select again the OK button to close the window.
- Click on the Edit Column button from the right side and add the name
Passedin the ColumnName field. - Set the Data Type as Int32.
- Select the check box for the Allow Null option for allowing the robot to take into consideration the null values.
- Select the OK option for closing the window.
- Select again the OK button to close the window.
- In the Properties panel, add the variable
-
Add an Assign activity below the Build Data Table activity.
- Insert the condition
statisticsDt.Rows(0)("Failed")in the To field and the variablefailedStudentsin the Value field.
- Insert the condition
-
Add another Assign activity below the previous one.
- Insert the condition
statisticsDt.Rows(0)("Passed")in the To field and the variablepassedStudentsin the Value field.
- Insert the condition
-
Place an Excel Write Range activity underneath the Assign activity.
- In the Properties panel, add the name
"Sheet1"in the SheetName field, the value"D1"in the StartingCell field and the variableresultsDtin the DataTable field.
- In the Properties panel, add the name
-
Add a new Excel Write Range activity and place in below the first one.
- In the Properties panel, add the name
"Sheet2"in the SheetName field, the value"A1"in the StartingCell field and the valuenew DataTable()in the DataTable field.
- In the Properties panel, add the name
-
Drag another Excel Write Range activity below the previous one.
- In the Properties panel, add the name
"Sheet2"in the SheetName field, the value"A1"in the StartingCell field and the variablestatisticsDtin the DataTable field. - Select the AddHeaders check box for the robot to consider the first row as header.
- In the Properties panel, add the name
-
Run the process. The robot reads the
.xlsxfile, analyzes the data, creates a new database and reorders the results as specified.