Click here to Skip to main content
14,927,707 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
HI..
i want to fetch column names of excel sheet in asp.net,
how should i do this?
thanks
Posted
Comments
Hiren solanki 27-Sep-10 2:25am
   
see my other updated link. may be useful

   
v2
Comments
niravsahayata 27-Sep-10 2:18am
   
thanks for this ..
i dont want to store excel sheet data to database...
i have excel sheet and i want to fetch only column names of excel sheet not whole data..
Nice to see this Question it's easy as I have recently accomplished project on Excel Worksheet.

Take my Work.

You could probably use OleDbProvider to meet to your requirement rather the using Interop COM Componant.

Here the Code Goes.

C#
protected void btnUpload_Click(object sender, EventArgs e)
    {
        OleDbConnection objConnection = new OleDbConnection(ConnectionString());
        objConnection.Open();
        OleDbCommand objCmdSelect = new OleDbCommand("SELECT * FROM [" + SheetName + "$]", objConnection);
        OleDbDataAdapter objAdapter = new OleDbDataAdapter(objCmdSelect);
        DataTable Dt = new DataTable();
        objAdapter.Fill(Dt);
        objConnection.Close();

    }
    private String ConnectionString()
    {
        return @"Provider=Microsoft.Jet.OLEDB.4.0;" +
            @"Data Source=" + fupdControl.PostedFile.FileName + ";" +
            @"Extended Properties=Excel 8.0;";
    }


Here input the SheetName and ExcelFileLocation it will retrieve all the data of that sheet into datatable, the column name of the datatable will be the column name of the Sheet.

http://www.codeguru.com/forum/archive/index.php/t-388883.html[^]

Please Vote or Accept Answer if it Helped.
   
v2
Comments
niravsahayata 27-Sep-10 2:16am
   
thanks for this,
i have excel sheet...but i want only column names of sheet not sheet data...
NMehta83 27-Sep-10 7:46am
   
reference to above code written by Hiren now you can add below logic to fetch the column name.
foreach (DataColumn column in dt.Columns)
{
Columns += column.ColumnName.ToString() + ",";
}
Columns = Columns.Substring(0, Columns.Length - 1);
Hiren solanki 27-Sep-10 7:47am
   
Thumbs up, MMehta83.
protected void btnUpload_Click(object sender, EventArgs e)
    {
        OleDbConnection objConnection = new OleDbConnection(ConnectionString());
        objConnection.Open();
        OleDbCommand objCmdSelect = new OleDbCommand("SELECT TOP 1 * FROM [" + SheetName + "$]", objConnection);
        OleDbDataAdapter objAdapter = new OleDbDataAdapter(objCmdSelect);
        DataTable Dt = new DataTable();
        objAdapter.Fill(Dt);
        foreach(DataColumn dc in dt.Columns)
        {
         lblColumNames.Text +=  dc.ColumnName + ",";
        }
        objConnection.Close();
    }
    private String ConnectionString()
    {
        return @"Provider=Microsoft.Jet.OLEDB.4.0;" +
            @"Data Source=" + fupdControl.PostedFile.FileName + ";" +
            @"Extended Properties=Excel 8.0;";
    }
   
OleDbConnection objCx = new OleDbConnection ();
OleDbConnection objCx = new OleDbConnection(cxString);
objCx.Open();
String[] restrection = { null, null, "Sheet1$", null };
dt = objCx.GetSchema("Columns", restrection);
String[] str = new String[dt.Rows.Count];
for (int i = 0; i <
dt.Rows.Count; i++)
{
str[i] = dt.Rows[i].ItemArray[3].ToString();

}
   

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