Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have taken all columns from xls file. Actually xls file cointain 10 columns but i received more than 10 columns like F12,F13 Like this. So, how i can received exact 10 columns.

<code>
C#
public static string[] GetColumnsName(DataTable dt)
        {
            string[] columnNames = null;
            if (dt != null && dt.Rows.Count > 0)
            {
               columnNames = dt.Columns.Cast<DataColumn>()
                               .Select(x => x.ColumnName)
                               .ToArray();
            }
            return columnNames;
        }

Posted
Comments
Naveen Kumar Tiwari 12-May-15 2:38am    
You can achieve this using excel C# interop class..
Kornfeld Eliyahu Peter 12-May-15 2:40am    
Can you show the exact query you run to get dt?
You need to apply logic where you get that DataTable dt.
AshishvermaMCA 12-May-15 3:18am    
I am using this code to take data from excel file-


oledbConn.Open();
object dt = oledbConn.GetSchema("Tables").Rows[0]["TABLE_NAME"];

OleDbCommand cmd = new OleDbCommand("SELECT * FROM [" + dt.ToString() + "]", oledbConn);
OleDbDataAdapter oleda = new OleDbDataAdapter();
oleda.SelectCommand = cmd;
oleda.Fill(ds, "WellCareMCD");
oledbConn.Close();

Follow this link...May this help You....
How to Fixed the Excel Column Width[^]


Happy Coding
 
Share this answer
 
AshishvermaMCA wrote in comment:
I am using this code to take data from excel file-

C#
oledbConn.Open();
object dt = oledbConn.GetSchema("Tables").Rows[0]["TABLE_NAME"];
OleDbCommand cmd = new OleDbCommand("SELECT * FROM [" + dt.ToString() + "]", oledbConn);
OleDbDataAdapter oleda = new OleDbDataAdapter();
oleda.SelectCommand = cmd;
oleda.Fill(ds, "WellCareMCD");
oledbConn.Close();


Replace:
C#
OleDbCommand cmd = new OleDbCommand("SELECT * FROM [" + dt.ToString() + "]", oledbConn);

with:
C#
OleDbCommand cmd = new OleDbCommand("SELECT ColumnName1, ColumnName2, ColumnNameX FROM [" + dt.ToString() + "]", oledbConn);


EDIT
For dynamic version, please see:
Excel Add-in Framework for Validating and Exporting Data[^]
It might be helpful too: Reading an Excel Sheet into a DataTable, generic method[^]
 
Share this answer
 
v2
Comments
AshishvermaMCA 12-May-15 3:42am    
No I can't replace - SELECT * FROM [" + dt.ToString() + "] with SELECT ColumnName1, ColumnName2, ColumnNameX FROM [" + dt.ToString() + "]

Actually my objective is When i am going to upload Excel file need to check excel columns is exact with my column which is used in code like
ds.Tables[0].Rows[i]["ColumnName1"].ToString();
AshishvermaMCA 12-May-15 3:44am    
Hi Maciej Los,
I will face one more problem when i an using OleDbCommand cmd = new OleDbCommand("SELECT ColumnName1, ColumnName2, ColumnNameX FROM [" + dt.ToString() + "]", oledbConn);


because if user change the column name in Excel file then how i will handle this.
Maciej Los 12-May-15 3:50am    
OK, you need to use "dynamic" version. I'll try to update my answer.
AshishvermaMCA 12-May-15 8:00am    
Thanks Maciej Los
For giving solution.
Maciej Los 12-May-15 8:28am    
You're very welcome ;)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900