How to give column name selected data

Hi Everyone,
I want to give them column names and make it as a datatable, how can I turn them into columns
Thanks in advance…

1 Like

you can read the data in a datatable, make sure you uncheck the Has Headers checkbox.

in code you can create a datatable using Build Datatable and give the required headers.

Then you can simply merge these two datatable. you can use Merge Datatable activity for this

Hi

Hope the below steps would help you resolve this

  1. First read this excel with a excel application scope where pass the filepath as input and inside the scope use a READ RANGE activity and mention the cell range of the table
    For example Like “B11:H50” and mention the sheet name and disable the property ADD HEADERS in the property panel

  2. Get the output from read range activity as a datatble named dt

  3. Now use a assign activity like this to give your own name to each column

dt.Columns(“yourcolumnname”).ColumnName = “your new columnname”

Like wise use n number of assign you want based on the number of columns you want to name

  1. Then use a WRITE RANGE activity and pass the dt variable as input and enable ADD HEADERS property only then the new column name will reflect in excel

Cheers @Daniyal_Tahir

1 Like

@rahulsharma its printing only the number of rows but blank

@Palaniyappan what should I write in “YourColumnName”, as there is no no column name in my sheet

1 Like

in addition to

you can do a generic datacolumn naming by following:


TypeArgument: DataColumn

It will create columnnames like Col0,Col1,… as much Columns are present within the datatable
The naming schema can be adopted as well

E.g.
col.ColumnName = UiPath.Excel.Helpers.ExcelUtilities.ConvertColumnIndexToColumnLetter(idx + 1)

will create Columnname like A,B,C, …AA,AB,…

We would also recommend to do some initial filterings on empty rows and unneeded columns

1 Like

Usually when you are reading a table with ADD HEADERS column disabled it will create the datatable with column name as Column-1,
Column-2 like that

We can even cross check with a simple writeline activity like this to know the first column name

dt.Columns(0).ColumnName.ToString

Or

On a easy not if we don’t know the column name mention it’s index position
For first column we can mention as
dt.Columns(0).ColumnName

For second column
dt.Columns(1).ColumnName

So it starts with 0 for first column

Cheers @Daniyal_Tahir

can you share the xaml and file?

It should not show any blanks if you read the data properly. Make sure when you read t excel, you use proper range to get t data in Datatable

Yup, It worked, Thank you for helping everyone

1 Like

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