Click here to Skip to main content
15,906,625 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to make program so as to open an excel file , read it and save it as a database using 'oledb' i tried many codes and i check for solution on google and try to built a code from scratch but i get nothing ,so i want help ???
Posted

1 solution

If you don't want to use the Excel COM objects, you can use OleDb. It takes a little setup in your Excel document. Basically, you need to define "named objects" in Excel that are synonymous to tables in a database. The first row of the named object are the column headers. To set up a named object, first select the range of cells (your "table," with the first row being the column headers), then go to menu Insert->Names->Define. Name your object and press "Add." Now you have an object which can be read by ADO.NET.

Now for the C# (this example assumes I have an Excel file at C:\Book1.xls and a named object in this workbook called "MyObject"):


C#
using System.Data;
using System.Data.OleDb;

...
C#
OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Book1.xls;Extended Properties=Excel 8.0");
OleDbDataAdapter da = new OleDbDataAdapter("select * from MyObject", con);
DataTable dt = new DataTable();
da.Fill(dt);


Now you can save this data table into DB.

To transform the contents of excel into array:

C#
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;
    string[] strArray = ConvertToStringArray(myvalues);
    }


Now you can iterate through the array[].

Edit: Code formatted
 
Share this answer
 
v3
Comments
samira Magdy 7-Sep-11 10:12am    
i alreaday connect the excel with the database with using excel interop library but all i want to do know is to read each cell in the excel sheet because of i want to write a query to retrieve a cell from this sheet ,so i want to save the content of the excel so as to check it and make a query on it
Pradeep Shukla 7-Sep-11 10:20am    
You can iterate through the data table to get the values of the cell or transform the data into arrays to read through them..I have added small code snippet for that.
samira Magdy 7-Sep-11 10:31am    
mmmmm, sorry but i can't understand your code i want a small explanation because this idea is very helpful and i'll try it thnx for your help
Pradeep Shukla 7-Sep-11 10:47am    
Is it the code in the for loop that you need explanation:
Here the columns traversed are limited to 10 ( column A to column J ), you can change it to a different number. The method Get_Range() extracts the data and puts in an array.
samira Magdy 13-Sep-11 8:39am    
thnx alot, your code help me alot in solving this problem

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