retrieve excel sheet column names in c# (winforms)..

Posted by Gopal_nivas under C# on 10/11/2009 | Views : 14814 | Status : [Member] | Replies : 4
hi..

i have a excel sheet, in that i have specified three columns namely id,name,salary .

my question is i want to retrieve that cloumn names (in excel sheet) to a listbox control in windows application using c#..


how to do this...
need ur suggestions

regards
gopal.s




Responses

Posted by: Vuyiswamb on: 10/12/2009 [Member] [MVP] [Administrator] NotApplicable

Up
0
Down
Look at this

http://www.dotnetspider.com/forum/226234-Retrieve-excel-sheet-column-names-winforms-using-c.aspx.aspx


Thank you for Posting at DotnetFunda

Vuyiswa Maseko

Thank you for posting at Dotnetfunda
[Administrator]

Gopal_nivas, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Tridev_2005 on: 10/12/2009 [Member] Starter

Up
0
Down
Try this code sample
----------------------
string filename = @"E:\234.xls";
string sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filename + ";Extended Properties=Excel 8.0;";
OleDbConnection objConn = new OleDbConnection(sConnectionString);
objConn.Open();
DataTable dte = null;
DataSet ds = new DataSet();
dte = objConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
string fname = dte.Rows[0].ItemArray[2].ToString();
OleDbCommand objCmdSelect = new OleDbCommand("SELECT name,[phone no] FROM [" + fname + "]", objConn);
OleDbDataAdapter objAdapter1 = new OleDbDataAdapter();
objAdapter1.SelectCommand = objCmdSelect;
objAdapter1.Fill(ds, "xldata");
objConn.Close();

Gopal_nivas, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Dinee_432 on: 10/13/2009 [Member] Starter

Up
0
Down
Hi,

I am new to the windows application.
I am working in dotnet coding through C#.
I dont have any idea about how to read the datas from an ecxel file and populate it in a datagrid in windows form.
Please help me out in detail.
Its really urgent.
Thanks in advance

Gopal_nivas, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Evgenik on: 11/12/2009 [Member] Starter

Up
0
Down
Hi.
These are small tips for you:

public void OpenDocument(string filename)
{
WorkBooks = oExcel.GetType().InvokeMember("Workbooks", BindingFlags.GetProperty, null, oExcel, null);
WorkBook = WorkBooks.GetType().InvokeMember("Open", BindingFlags.InvokeMethod, null, WorkBooks, new object[] { name, true });
WorkSheets = WorkBook.GetType().InvokeMember("Worksheets", BindingFlags.GetProperty, null, WorkBook, null);
WorkSheet = WorkSheets.GetType().InvokeMember("Item", BindingFlags.GetProperty, null, WorkSheets, new object[] { 1 });
}

public void SaveDocument(string filename)
{
if (File.Exists(name))
WorkBook.GetType().InvokeMember("Save", BindingFlags.InvokeMethod, null,WorkBook, null);
else
WorkBook.GetType().InvokeMember("SaveAs", BindingFlags.InvokeMethod, null, WorkBook, new object[] { name });
}

public void SetValue(string range, string value)
{
Range = WorkSheet.GetType().InvokeMember("Range", BindingFlags.GetProperty, null, WorkSheet, new object[] { range });
Range.GetType().InvokeMember("Value", BindingFlags.SetProperty, null, Range, new object[] { value });
}

Use with this:
OpenDocument(filePath);
SetValue("A1", "your value");
CloseDocument();

Good luck.
Evgeni

Gopal_nivas, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response