Get Excel columns

I have a table with the following 3 columns:
“Name”, “Age”, “City”. I extract the names of the columns with this expression:

(From d in dtTable.Columns.Cast(of system.data.dataColumn) Select d.ColumnName).toArray

It works fine and returns the name of the columns in an array variable, but sometimes it returns the name of my columns plus this:

“Name”,“Age”,“City”,“Column1”,“Column2”,“Column3”…

How I can avoid this?

Hello @Marisa_Ontiveros1

Please delete the columns directly from excel. there might be some empty spces in the other three columns. so it’s returning when you use read range.

Cheers,
Mounika

@Marisa_Ontiveros1,

Add this logic before you retrieve the columns.

This will remove any additional non required columns from datatable.

Thanks,
Ashok :slight_smile:

1 Like

We can filter out the autogenerated columns by:

(From c in dtTable.Columns.Cast(of System.Data.DataColumn) 
Let cn = c.ColumnName
Where Not cn.StartsWith("Column")
Select v=cn).toArray
1 Like

It`s great!!! Works!!! Trank you very much

The Excel file you’re reading has columns that don’t have a header, so it’s automatically naming them Column1, Column2, etc

@Marisa_Ontiveros1,

Glad I’m able to help you. Please Mark my answer as solution so it will be helpful to other community members.

Thanks,
Ashok :slightly_smiling_face:

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