Create C# Class File Using UiPath (RPA) By Reading Excel

In this article, you will learn about UiPath Studio a RPA tool to read Excel and create C# class files.

In this article, we will be able to create a C# file using sequence in UiPath Studio by reading content from the Excel file. We are creating a model class whose properties with datatype are defined in  Excel, like below.
 
Create C# Class File Using UiPath (RPA) By Reading Excel

Generally, UiPath Studio allows you to create two types of projects - process and library. Processes can incorporate all types of workflows, sequence, flowchart... We will create a process and give it a nice name.

Create C# Class File Using UiPath (RPA) By Reading Excel
 
At the moment, we can use VB with a full extent but not C#, as it is not fully supported.
 
Create C# Class File Using UiPath (RPA) By Reading Excel
 
Click the “Create” button; it will land you on this:

Create C# Class File Using UiPath (RPA) By Reading Excel
 
By default, we have “Main.xaml” ready to use, you can add a new file in the project, create a sequence to read the Excel. 
 
Create C# Class File Using UiPath (RPA) By Reading Excel

Create C# Class File Using UiPath (RPA) By Reading Excel
 
To read Excel, we have to use the “Excel Application Scope” activity, which you can find in the Activity tab. You can simply search it in the search box or go to App Integration => Excel and you will find the activity.
 
If you can’t find it there then you can download the package called “UiPath.Excel.Activities” from “Manage Packages” available in the top menu.
 
To use this package you don't need Microsoft Excel, there are many more functionalities of this package, I will write a separate article in the future for that.
 
Drag our required activity inside the sequence, then select a file from browse option or simply type file name with double-quote like, "CSharpCornerDemo.xlsx"
 
To store excel data we need to create UiPath.Excel.WorkBookApplication variable can create it in the “Variables” tab just like,
 
Create C# Class File Using UiPath (RPA) By Reading Excel
 
Then, you can go to the properties of this activity in the right section.
 
 
Click inside textbox next to Output=> Workbook and select the variable we just created.
 
Create C# Class File Using UiPath (RPA) By Reading Excel
 
There is another way to create variable directly inside textbox next to Output=> Workbook, press “ctrl+K” Creates a variable of the same type as the required type of the activity. This is the best approach as sometimes we are not sure what data type is required for the activity.
 
Then add “Assign” activity inside “Do”, create variable “sheetNames” of IEnumerable type so we can get different sheet names from excel using Method “GetSheets”.
 
Create C# Class File Using UiPath (RPA) By Reading Excel
 
Then add “For Each” activity below “Assign”, be careful while dragging activity because the scope of that activity might change if wrongly placed, for example, consider the below two ways,
  • The right side approach will mostly work because both “Assign” and “For Each” are present inside "Do".
  • In the left side approach, we can’t access variable “sheetNames” outside “Do” activity as its scope is defined in that way. If you mouse over on that blue warning like icon it will give you a detail error.
Create C# Class File Using UiPath (RPA) By Reading Excel
 
Note

We can access any variable anywhere but we have to change its scope before from the “Variable” tab. Now, read each sheet using “Read Range”, don’t forget to create an output variable whenever necessary.

Create C# Class File Using UiPath (RPA) By Reading Excel

Create one more variable and assign below value, "public class "+item.ToString & vbNewLine & "{" As we are using VB as backend so “& vbNewLine &” means new line.

Create C# Class File Using UiPath (RPA) By Reading Excel
 
Add activity called “For Each Row” which is used to loop on DataTable.
 
Add the “Assign” inside the loop, here, we are concatenating the string which we create in above assign activity.

Create C# Class File Using UiPath (RPA) By Reading Excel
 
Note
We can assign values from properties window like,
 
Create C# Class File Using UiPath (RPA) By Reading Excel
 
Again concatenate below value using “Assign” outside the “For Each Row”, as it is the closing braces of the class.
 
stringVar & vbNewLine & "}"
 
Add “Append Line” to sequence, as the name suggests, it will append the given string to your desired file.
 
Just put the folder location add file next to it like,
 
"D:\SequenceToCreateCSharpFile\"+item.ToString+".cs"
 
Create C# Class File Using UiPath (RPA) By Reading Excel

Click on Start from the top menu and enjoy it. The output .cs file is like,
 
Create C# Class File Using UiPath (RPA) By Reading Excel
 

Summary

 
We have created a process project in UiPath Studio, then added a sequence so we can use our required activities to read the Excel and then create .cs file from that data.
Activities; we have used in this project are.
I hope you have got a good idea about what I have explained in the article, though it’s a basic but we can achieve a more complicated process though this. We will try to explain what next steps we can do using this article as a base.
 
Stay tuned for more...