none
Read Excel(xlsx) file from c# console application RRS feed

  • Question

  • Hi,

        I am developing console application where it is required read xlsx file and do proccessng.

        I am trying to connect to excel file using below connection string

        Provider=Microsoft.ACE.OLEDB.12.0; Data Source="FileName";Extended Properties=Excel 12.0 XML;

         My development environment details are as below

         1) Windows 2012 R2

         2) Office 2016 (32 bit)

         3) Visual studio 2015

          Case 1: If I am using Target platform of application as x86,  application is able to read excel successfully.

          Case 2: If I am using Target platform of application as x64/Any CPU, application throws exception mentioning oledb provider is not registered on machine.

                     But it is necessity to run application on 64 platform only. Can you please suggest how to resolve this issue.

    Thanks and regards,

    Shardul Pasare      

    Friday, January 26, 2018 3:40 PM

All replies

  • Hi Shardul Pasare,

    From the description of the thread, I can see that you are developing a Console Application.

    Your code is working fine but when you change the Target Platform from your Console Application it gives you an error.

    I think that this issue is more related with Console Application Development.

    So for better response and better solution for your issue, I move this thread to Visual C# Forum.

    The reason why we recommend posting appropriately is you will get the most qualified pool of respondents, and other partners who read the forums regularly can either share their knowledge or learn from your interaction with us. Thank you for your understanding.

    Regards,

    Deepak


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, January 29, 2018 2:18 AM
  • Hi Shardul Pasare,

    For your question, Please try the code below. I test the code on 64 platform, it works well. For better understanding, I show the excel value in datatable.

     string name = "Sheet2";          
                string constr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + @"New.xlsx" + "; Extended Properties = 'Excel 12.0;HDR=YES;IMEX=1;';";
                OleDbConnection con = new OleDbConnection(constr);
                OleDbCommand oconn = new OleDbCommand("Select * From [" + name + "$]", con);
                con.Open();
                OleDbDataAdapter sda = new OleDbDataAdapter(oconn);
                DataTable data = new DataTable();
                sda.Fill(data);
    

    >>Case 2: If I am using Target platform of application as x64/Any CPU, application throws exception mentioning oledb provider is not registered on machine.

    According to the error message, what is the version of your Microsoft Office Access database engine? Please install both 2010 and 2007.

    Microsoft Office Access database engine 2007
    https://www.microsoft.com/en-us/download/confirmation.aspx?id=23734

    Microsoft Access Database Engine 2010 Redistributable

    https://www.microsoft.com/en-us/download/details.aspx?id=13255

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, January 29, 2018 2:44 AM
    Moderator
  • Hello Wendy,

                 Sorry for late reply.

                 In my case it was case 2. So to avoid installation I required to change method of reading excel file. I have used DocumentFormat.xml to read excel file instead of oledb. 

                anyway thank you so much.

    Thanks and regards,

    Shardul Pasare

                

    Friday, March 16, 2018 3:42 PM