Author Topic: Determine Columns Name from an Excel file  (Read 5759 times)

Offline admin

  • Administrator
  • Sr. Member
  • *****
  • Posts: 296
    • View Profile
Determine Columns Name from an Excel file
« on: October 20, 2008, 12:05:19 PM »
If you'd like to Determine Columns Name from an Excel file just use the following function,
I hope it could help.
Thanks

Code: [Select]
public String[] GetExcelSheetColums(string filename){

OleDbConnection objConn = null;
System.Data.DataTable dt = null;

try
{

String cxString = "Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + filename + ";Extended Properties=Excel 8.0;";

objCx = new OleDbConnection(cxString);

objCx.Open();

String[] restrection = {null,null,"Sheet1$",null}

dt = objCx.GetOleDbSchemaTable(OleDbSchemaGuid.Columns, restrection);

if(dt == null)
{
return null;
}

String[] excelSheets = new String[dt.Rows.Count];
int i = 0;


foreach(DataRow row in dt.Rows)
{
excelSheets[i] = row["a_NAME"].ToString();
i++;
}


for(int j=0; j < excelSheets.Length; j++)
{

// you the column name to deal with your database!!!!
}

return excelSheets;
}
catch(Exception ex)
{
return null;
}
finally
{
Clean up.
if(objConn != null)
{
objConn.Close();
objConn.Dispose();
}
if(dt != null)
{
dt.Dispose();
}
}
}