Click here to Skip to main content
15,890,557 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
Hi guys,

Please help me, I want to insert data of one excel sheet’s particular row and column data in database. I know I can use “OleDbConnection” connection, but it give me whole sheet data not portion of sheet. So please help me in this case.

Thanks in advance.


Thanks guys,
I really appreciate your valuable suggestions, but Om Prakash Pant I try your suggestion and it give me error. I know that your mention code is not complete it only a way for my goal which not achieved by only this. Can you please guide me on your solutions more I really thank full to you.

ahsan sarfraz I already try this but data is raw then some data become column name so it not possible to use this solution.
Guys, I use window application right now so please suggest me on that aria and my goal is fetch data from Excel file and fill the dataset then update those dataset in database.

Is there any other solution please provide me I really need it.
Posted
Updated 14-Apr-11 9:04am
v2

If the data is not uniform but the row and column is fixed then you can use Excel Objects Library instead of OleDB provider. something like the below code:

Excel.Workbook theWorkbook = ExcelObj.Workbooks.Open(openFileDialog1.FileName, 0, true, 5,
          "", "", true, Excel.XlPlatform.xlWindows, "\t", false, false,
          0, true); 
     Excel.Sheets sheets = theWorkbook.Worksheets;
     Excel.Worksheet worksheet = (Excel.Worksheet)sheets.get_Item(1);
     for (int i = 1; i <= 10; i++)
     {
     Excel.Range range = worksheet.get_Range("A"+i.ToString(), "J" + i.ToString());
     System.Array myvalues = (System.Array)range.Cells.Value;
}


If the data is formatted and has sequential data then you can use OleDB provider.
 
Share this answer
 
v2
Comments
angle57 14-Apr-11 7:46am    
thanks Om Prakash Pant,
but it not work in my system, can you please give me more code for this . and one thing right now i try it with window application with c# in .net 4.0. i am very thanks full to you.
hi,
you should get the data from OLEDB connection from the excel sheet. Populae the data in a datatable/Dataset and then you can filter the rows from the dataset and insert the ones you want and leave the ones you dont want...
the point is get the whole data and then filter rather then filter and then retrieve.
hope it helps.
best of luck
 
Share this answer
 
string Con_Str = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + FullPath + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\";";
                    OleDbConnection con = new OleDbConnection(Con_Str);

                    String qry = "SELECT * FROM [Sheet1$]";
                    OleDbDataAdapter odp = new OleDbDataAdapter(qry, con);
                    DataSet ds = new DataSet();
                    odp.Fill(ds);
 
Share this answer
 
string Con_Str = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + FullPath + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\";";
                    OleDbConnection con = new OleDbConnection(Con_Str);

                    String qry = "SELECT * FROM [Sheet1$]";

                    OleDbDataAdapter odp = new OleDbDataAdapter(qry, con);
                    DataSet ds = new DataSet();
                    odp.Fill(ds);


Note: Using ds you can perform desire operation. 
 
Share this answer
 
simply get your whole excel sheet in dataset or datatable
then u can filter dataset according to ur condition like if using dataset

ds.Tables[0].Rows[0][columnname].tostring()
 
Share this answer
 

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