Manage Multiple Excel Files
The example below explains how to create a single excel report file by extracting data from multiple excel report files that have the same structure. It presents activities such as Excel Application Scope, Remove Duplicates Range, Save Workbook, Close Workbook, Read Range and Append Range. 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.
- Go to the New tab and select Sequence.
- In the Name field write
MergeReports. - Choose to save the process in the same location as the Main process.
- Select Create.
- In the Name field write
In the project folder add the ReportTemplate.xlsx file and create a new Reports folder that includes the following .xlsx files: Report1, Report2 and Report3.
-
In the Workflow Designer, select the MergeReports tab.
-
Drag a For Each activity to the Workflow Designer.
- In the ForEach field add the value
file. - In the Properties panel, set the TypeArgument field to
Stringand addReportFilesin the Values field.
- In the ForEach field add the value
-
Drag a Sequence container in the body of the For Each activity.
- Create a new DataTable variable named
ReportTable. - Create a new variable named
ReportTableand of type DataTable.
- Create a new DataTable variable named
-
Add an Excel Application Scope activity inside the Sequence.
- In the Properties panel, add the name
Report filein the DisplayName field and the valuefilein the WorkbookPath field. - Select the check boxes for the ReadOnly and Visible options. This makes the file visible and read only to any user that accesses it without a password.
- In the Properties panel, add the name
-
Drag an Excel Read Range activity and add it inside the Excel Application Scope activity.
- In the Properties panel, add the value
ReportTablein the DataTable field. - Select the check box for the AddHeaders option so that the first row of the table to be identified as a header.
- In the Properties panel, add the value
-
Drag a new Excel Application Scope activity and place it below the Excel Application Scope activity named
Report file.- In the Properties panel, add the name
Output reportin the DisplayName field and the valueOutputReportin the ExistingWorkbook field. - Select the check box for the Visible option. This creates the file visible for any user.
- In the Properties panel, add the name
-
Add an Excel Append Range activity in the body of the Excel Application Scope activity.
- In the Properties panel, add the variable
ReportTablein the DataTable field and"Sheet1"in the SheetName field.
- In the Properties panel, add the variable
-
Drag an Assign activity below the Excel Application Scope activity named
Output report.- In the Properties panel, add the variable
RowsCounterin the To field andRowsCounter + ReportTable.Rows.Countin the Value field.
- In the Properties panel, add the variable
-
In the Workflow Designer, select the Main tab.
-
Drag a Sequence container to the Workflow Designer.
- Create the following variables:
Variable Name Variable Type Default Value RowsCounterInt32 N/A OutputReportNameString N/A ReportFilesArray of Strings N/A OutputReportWorkbookApplication N/A -
Add an Assign activity in the body of the Sequence.
- In the Properties panel, add
OutputReportNamein the To field andString.Format("Report-{0:yyyyMMddHHmmss}.xlsx", DateTime.Now)in the Value field. This includes the current date and time in the file name.
- In the Properties panel, add
-
Add a Copy File activity and place it below the Assign activity.
- In the Properties panel, add the variable
ReportTemplatePathin the Path field and the variableOutputReportNamein the Destination field.
- In the Properties panel, add the variable
-
Drag a new Assign activity under the Copy File activity.
- Add the variable
ReportFilesin the To field and the syntaxDirectory.GetFiles(ReportsFolderPath, "*.xlsx")in the Value field.
- Add the variable
-
Drag an Excel Application Scope after the Assign activity.
- In the Properties panel, add the variable
OutputReportNamein the WorkbookPath field, addOutputReportin the Workbook field and select the check box of the Visible option for making the file visible for everyone.
- In the Properties panel, add the variable
-
Add a Sequence container in the body of the Excel Application Scope activity.
-
Add an Invoke Workflow File inside the newly created Sequence.
- Set the path of the WorkflowFileName as
"MergeReports.xaml".
- Set the path of the WorkflowFileName as
-
Drag a Remove Duplicates Range activity and place it under the Invoke Workflow File activity.
- In the Properties panel, add
"A2:F" + (RowsCounter + 1).ToStringin the Range field and"Sheet1"in the SheetName field.
- In the Properties panel, add
-
Drag a Save Workbook activity under the Remove Duplicates Range activity.
-
Add a Close Workbook activity underneath the Excel Application Scope activity.
- Add the variable
OutputReportin the Workbook field.
- Add the variable
-
Run the automation process. Once completed you should have a new
.xlsxfile that uses the predefined template and incorporates data from multiple files.