UiPath Activities

Filter and Delete Rows in CSV Files

The example below explains how delete unwanted rows from an .xlsx file and how to filter the information by applying specific criteria. It presents activities such as Remove Data Row, Read Range or Write Range. You can find these activities in the UiPath.Excel.Activities package.

The .xlsx file used in the example includes the following columns: Name, Km, Price, and SellerType. The automation process deletes all the rows that contain the value Other and Private Seller from the SellerType column and creates a new sheet with the filtered results.

This is how the automation process can be built:

  1. Open Studio and create a new Process named by default Main.

Note:

Make sure to add the .xlsx file that you want to use in the project folder. You can also use the file we provided by downloading the example available at the bottom of this page.

  1. Drag a Flowchart in the Workflow Designer.
    • Create the following variables:
Variable Name
Variable Type
Default Value

carsList

WorkbookApplication

-

inputList

System.Data.DataTable

-

  1. Drag an Excel Application Scope activity in the Flowchart and connect it to the Start Node.
    • Double click on the Excel Application Scope activity to open it.
    • In the Properties panel, add the name Delete Data in the DisplayName field.
    • Add the path of the .xlsx file in the WorkbookPath field, in this case, Cars.xlsx.
    • 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 variable carsList in the Workbook field.
  2. Select the Do sequence inside the Excel Application Scope activity and create the following variable:
Variable Name
Variable Type
Default Value

filterList

System.String[]

{"Private Seller", "Other"}

  1. Drag an Excel Read Range activity inside the Do sequence.
    • In the Properties panel, add the name "Cars" in the SheetName field.
    • Select the AddHeaders check box for considering the first row of the table a header.
    • Add the variable inputList in the DataTable field.
  2. Place a For Each activity below the Read Range activity.
    • Add the value word in the ForEach field.
    • In the Properties panel, select the option String from the TypeArgument drop-down list and add the variable filterList in the Values field.
  3. Select the Body container inside the For Each activity and create the following variables:
Variable Name
Variable Type
Default Value

query

String

-

RowsToDelete

System.Data.DataRow[]

-

deletedRows

Int32

-

  1. Drag an Assign activity inside the Do container.
    • Add the variable query in the To field and the expression string.Format("[SellerType]='{0}'", word) in the Value field. This returns the matching values in the SellerType column and stores them in the query variable. The string.Format method is used to convert the DataRow objects that match the filter words to strings.
  2. Drag another Assign activity and place it below the first one.
    • Add the variable RowsToDelete in the To field and the expression inputList.Select(query) in the Value field.
  3. Add a third Assign activity under the first two Assign activities.
    • Add the variable deletedRows in the To field and the expression RowsToDelete.Count-1 in the Value field. This creates a counter for the rows that needs to be deleted.
  4. Place a While activity under the Assign activities.
    • Insert the expression deletedRows>=0 in the Condition field. This means that the activities in the Body section are performed until the value of the variable deletedRows equals 0.
  5. Drag a Remove Data Row activity to the Body section of the While activity.
    • Add the variable inputList in the DataTable field.
    • Add the expression RowsToDelete(deletedRows) in the Row field.
  6. Place an Assign activity below the Remove Data Row activity.
    • Add the variable deletedRows in the To field and the expression deletedRows-1 in the Value field. This creates a loop that is executed until the value of the variable deletedRows equals 0.
  7. Return to the Flowchart workflow.
  8. Drag an Excel Application Scope activity and connect it to the previous Delete Data Excel Application Scope activity.
    • Double click on the Excel Application Scope activity to open it.
    • In the Properties panel, add the name Filter Data in the DisplayName field.
    • Select the check boxes for the AutoSave and CreateNewFile options. The robot is now allowed to create a new Excel workbook and to automatically save all the changes made to it.
    • Add the variable carsList in the Workbook field.
  9. Drag a Close Application activity inside the Do container of the Excel Application Scope activity.
    • Open the .xlsx file that you are using for this automation. For this example is Cars.xlsx.
    • Click on the Options Menu button, select the Indicate on screen option and indicate the .xlsx file.
  10. Drag an Excel Write Range activity below the Close Application activity.
    • In the Properties panel, add the name "Cars2" in the SheetName field.
    • Add the variable inputList in the DataTable field and the path "Cars.xlsx" in the WorkbookPath field.
    • Select the AddHeaders check box for considering the first row of the table a header.
  11. Run the process. The automation process deletes the specified data and filters the information in a new workbook sheet.
     
     
    Download example

Filter and Delete Rows in CSV Files


Suggested Edits are limited on API Reference Pages

You can only suggest edits to Markdown body content, but not to the API spec.