Developers Heaven Forum
News: To still receiving newsletters from us please subscribe to our Newsletters:
http://tech.groups.yahoo.com/group/developers-Heaven/
 
*
Welcome, Guest. Please login or register.
Did you miss your activation email?
March 18, 2010, 10:56:49 AM


Login with username, password and session length


Pages: [1]   Go Down
  Print  
Author Topic: Determine Columns Name from an Excel file  (Read 380 times)
0 Members and 1 Guest are viewing this topic.
admin
Administrator
Full Member
*****
Offline Offline

Posts: 206


OS:
unknown
Browser:
unknown


View Profile
« 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:
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();
}
}
}
Logged
Pages: [1]   Go Up
  Print  
 
Jump to:  

Google
   
Powered by MySQL Powered by PHP Powered by SMF 1.1.4 | SMF © 2006-2007, Simple Machines LLC

Copyright © Developers-Heaven.net 2008. All rights reserved.
Valid XHTML 1.0! Valid CSS! Dilber MC Theme by HarzeM