excel to access

Last post 05-07-2008 10:55 PM by smiling4ever. 6 replies.

Sort Posts:

  • excel to access

    05-06-2008, 3:27 AM
    • Loading...
    • gr33nhorn
    • Joined on 05-04-2008, 7:23 AM
    • Posts 10

    what is the code to import excel data  to access database using c#??

  • Re: excel to access

    05-06-2008, 3:47 AM
    Answer

    Hi,

    There is no direct way to do that. All what you have to do is to open the excel file and to iterate through the records using the datareader for example, and to populate the access db with the value.

    Code below to open an excel file

     

     DataSet myDataset = new DataSet();
                        string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=file.xls;" + "Extended Properties=Excel 8.0;";
    
                        //'You must use the $ after the object you reference in the spreadsheet
                        OleDbDataAdapter myData = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", strConn);
                        myData.TableMappings.Add("Table", "ExcelTest");
                        myData.Fill(myDataset);
     
     

     

    HTH
    Regards

  • Re: excel to access

    05-06-2008, 3:50 AM
    • Loading...
    • gr33nhorn
    • Joined on 05-04-2008, 7:23 AM
    • Posts 10

    THNXXX
  • Re: excel to access

    05-06-2008, 10:42 AM
    • Loading...
    • gr33nhorn
    • Joined on 05-04-2008, 7:23 AM
    • Posts 10

     

    @smiling4ever when i tried the code it gave error

    The Microsoft Jet database engine could not find the object 'Sheet1$'.  Make sure the object exists and that you spell its name and the path name correctly.

    at            

    myData.Fill(myDataset);

    plz help

    i have never used dataset

    is there something else i should also know?
     

  • Re: excel to access

    05-06-2008, 5:51 PM

    This is the excel sheet name, what is the sheet name in your excel document? 

  • Re: excel to access

    05-07-2008, 8:43 AM
    • Loading...
    • gr33nhorn
    • Joined on 05-04-2008, 7:23 AM
    • Posts 10

    sheet1

    i tried some other names too

    both in the sheet and the code

     

  • Re: excel to access

    05-07-2008, 10:55 PM

     Make sure you have the above provider and make sure that you are opening a supported xls file for the provider

Page 1 of 1 (7 items)