Line Breaks Handling in Excel Column Headers (Column Name is in multiple lines in cell)

Hello UiPath community,

I have a datatable “DT_data” which I use excel read range to get them. Then I would use For Each Row in “DT_data” to convert each row into a string.

For the column name which is in one line its fine, I manage to read the column and retrieve the row values in string. Which looks like this in the excel cell:

However there is error in the “For Each Row in DT_data” where it says the column “Date/time (UTC)” does not exist.

Then I realize the column header has multiple lines in one cell:

There is also examples with more than 2 lines in one cell:

I tried to copy paste the exact column name (copy from the cell) into my assign activity (with line breaks). But its not working. Still could not access the column name.

I also noticed the Column Name headers pattern are like this, many of them are in multiple lines in one cell.

How do I make sure my datatable headers are all in one line and only 1 white spaces between them? So that it would be easier for me to access the datatable for further processing.

Your help and suggestions are greatly appreciated.

Hi @Irfan_Musa

Here is a solution to change the Column Names of a data table by removing nextline characters:

The Process Flow:

  1. Use a For Each Activity.
  2. Under the In property: dt_Data.Columns. This will make the Item set to currentDataColumn automatically.

  3. Use an Assign Activity with the following Code:
currentDataColumn.ColumnName = currentDataColumn.ColumnName.Replace(Environment.NewLine, " ").Replace(vbCrLf, " ").Replace(vbLf, " ")

The Output:
As you can see in the below screenshot from the Immediate panel. The dt_Data before and after the for each/

If this solves your issue, Do mark it as a solution.
Happy Automation :star_struck:

Hi @Irfan_Musa

You can also use a LINQ expression inside an Invoke Code and get the same results.

The Code:

dt_Data.Columns.Cast(Of DataColumn)().ToList().ForEach(Sub(col)
    col.ColumnName = col.ColumnName.Replace(Environment.NewLine, " ").Replace(vbCrLf, " ").Replace(vbLf, " ")
End Sub)

The Activity:

The Argument for Invoke Code:

The Output:

If this solves your issue, Do mark it as a solution.
Happy Automation :star_struck:

Bạn có thể dùng Assign với LINQ trong UiPath để xóa các dòng trong DataTable A mà bị trùng với DataTable B.


Cách làm bằng Assign

Assign 1: Lọc DataTable A để loại bỏ các dòng trùng với B

dtA = dtA.AsEnumerable().
        Where(Function(rowA) Not dtB.AsEnumerable().
        Any(Function(rowB) rowA.ItemArray.SequenceEqual(rowB.ItemArray))).
        CopyToDataTable()

Giải thích

  1. dtA.AsEnumerable().Where(…) → Duyệt từng dòng trong dtA.
  2. Not dtB.AsEnumerable().Any(…) → Kiểm tra nếu dòng đó không tồn tại trong dtB.
  3. rowA.ItemArray.SequenceEqual(rowB.ItemArray) → So sánh tất cả các cột giữa rowArowB.
  4. CopyToDataTable() → Chuyển kết quả lọc thành DataTable mới.

Ví dụ

DataTable A (10 dòng)

DataTable B (20 dòng, chứa 7 dòng trùng với A)

Kết quả mong muốn (DataTable A sau khi xóa dòng trùng)


Xử lý nếu không còn dòng nào hợp lệ

Nếu tất cả các dòng trong dtA bị xóa, CopyToDataTable() sẽ báo lỗi. Để tránh lỗi này, dùng:

dtA = If(dtA.AsEnumerable().
         Where(Function(rowA) Not dtB.AsEnumerable().
         Any(Function(rowB) rowA.ItemArray.SequenceEqual(rowB.ItemArray))).
         Any(),
         dtA.AsEnumerable().
         Where(Function(rowA) Not dtB.AsEnumerable().
         Any(Function(rowB) rowA.ItemArray.SequenceEqual(rowB.ItemArray))).
         CopyToDataTable(),
         dtA.Clone())
  • Nếu còn dòng hợp lệ, giữ lại kết quả.
  • Nếu không còn dòng nào, trả về DataTable rỗng (dtA.Clone()).

Kết luận

Cách này giúp xóa hoàn toàn các dòng trong dtA nếu chúng trùng với dtB, và không thay đổi thứ tự của các dòng còn lại. Bạn thử áp dụng trong UiPath xem nhé!

Dưới đây là phiên bản đã thêm Trim() để loại bỏ khoảng trắng trước khi so sánh dữ liệu:

dtA = If(dtA.AsEnumerable().
Where(Function(rowA) Not dtB.AsEnumerable().
Any(Function(rowB) rowA.ItemArray.
Select(Function(x) x.ToString().Trim()).SequenceEqual(rowB.ItemArray.
Select(Function(x) x.ToString().Trim())))).
Any(),
dtA.AsEnumerable().
Where(Function(rowA) Not dtB.AsEnumerable().
Any(Function(rowB) rowA.ItemArray.
Select(Function(x) x.ToString().Trim()).SequenceEqual(rowB.ItemArray.
Select(Function(x) x.ToString().Trim())))).
CopyToDataTable(),
dtA.Clone())


Điểm cập nhật

Dùng .Select(Function(x) x.ToString().Trim()) trên ItemArray của cả rowA và rowB
→ Điều này giúp loại bỏ khoảng trắng đầu và cuối trên từng giá trị trong bảng trước khi so sánh.

Cấu trúc và logic không thay đổi
→ Vẫn đảm bảo nếu tất cả các dòng bị xóa, dtA.Clone() sẽ được sử dụng để tránh lỗi.


Bạn thử áp dụng vào UiPath xem kết quả có đúng theo mong muốn chưa nhé!

This method works! Thank you for your help!

1 Like

Nếu bạn muốn xóa tất cả các dòng trùng lặp nhưng giữ nguyên thứ tự của các dòng duy nhất trong dtA, bạn có thể sử dụng Assign với LINQ như sau:


Cách làm bằng Assign (Giữ nguyên thứ tự)

dtA = If(dtA.AsEnumerable().
GroupBy(Function(row) String.Join(“|”, row.ItemArray.Select(Function(x) x.ToString().Trim()))).
Where(Function(g) g.Count() = 1).
Any(),
dtA.AsEnumerable().
Where(Function(row) dtA.AsEnumerable().
Count(Function(r) String.Join(“|”, r.ItemArray.Select(Function(x) x.ToString().Trim())) =
String.Join(“|”, row.ItemArray.Select(Function(x) x.ToString().Trim()))) = 1).
CopyToDataTable(),
dtA.Clone())


Giải thích

  1. Dùng GroupBy(…) để xác định các nhóm dữ liệu giống nhau

String.Join(“|”, row.ItemArray.Select(Function(x) x.ToString().Trim()))

Loại bỏ khoảng trắng trước khi nhóm dữ liệu.

  1. Lọc ra các dòng có số lần xuất hiện = 1 (Count() = 1)

Where(Function(row) dtA.AsEnumerable().Count(…) = 1)

Giữ lại các dòng chỉ xuất hiện đúng một lần trong toàn bộ bảng.

  1. CopyToDataTable()

Chuyển danh sách các dòng hợp lệ thành DataTable.

  1. Xử lý khi không còn dòng hợp lệ (If(…))

Nếu vẫn còn dòng hợp lệ, lưu vào dtA.

Nếu tất cả bị xóa, trả về dtA.Clone() để tránh lỗi.


Ví dụ kiểm thử

Dữ liệu ban đầu (dtA)

Sau khi chạy LINQ trên (dtA kết quả)

:white_check_mark: Loại bỏ toàn bộ các dòng bị trùng
:white_check_mark: Giữ nguyên thứ tự của các dòng duy nhất
:white_check_mark: Trim khoảng trắng để tránh lỗi dữ liệu
:white_check_mark: Không bị lỗi nếu tất cả các dòng bị xóa

Bạn thử áp dụng trong UiPath xem kết quả có đúng như mong muốn không nhé!

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.