none
Error while Importing excel file to Datagridview

    Question

  • Hi, 

    I am trying to import an excel file File.xlsx. I am using Windows XP. When I try to import an excel file, I get the following error:

     

    Excel cannot open the file 'File.xlsx' because the file format or file extension is not valid. Verify that the file has not been corrupted and that the file extension matches the format of the file. at

    Microsoft.Office.Interop.Excel.Workbooks.Open(String Filename, Object UpdateLinks, Object ReadOnly, Object Format, Object Password, Object WriteResPassword, Object IgnoreReadOnlyRecommended, Object Origin, Object Delimiter, Object Editable, Object Notify, Object Converter, Object AddToMru, Object Local, Object CorruptLoad)

    if (Path.GetExtension("C:\xFile.lsx") == "xls")
    {
    excelConString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=File.xlsx"";Extended Properties='Excel 8.0;IMEX=1;'";
    }
    else
    {
    excelConString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source="File.xlsx";Extended Properties='Excel 12.0;IMEX=1;'";
    }
    
    using (OleDbConnection connection = new OleDbConnection(excelConString))
    {
    connection.Open();
    System.Data.DataTable dtSchema = new System.Data.DataTable();
    dtSchema = connection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
    object m = Type.Missing;
    Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
    Microsoft.Office.Interop.Excel.Workbook wbExcel = default(Microsoft.Office.Interop.Excel.Workbook);
    
    wbExcel = xlApp.Workbooks.Open((txtFile.Text.Trim()), m, false, m, m, m, m, m, m, m, m, m, m, m, m);

    But I am able to import File.xls. Where am I going wrong? 

    Request help urgently. 

    Thanks


    Monday, July 01, 2013 10:15 AM

Answers

  • 1.

    >excelConString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=File.xlsx"";Extended Properties='Excel 8.0;IMEX=1;'";

    Shouldn't this be "File.xls"

    2.

    Since you are using Office Automation, Could you please confirm if this system has xl2007+ installed?


    Sid

    Wednesday, July 03, 2013 6:26 AM

All replies

  • I believe you missed type the following C:\xFile.lsx

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.

    Monday, July 01, 2013 7:25 PM
  • Hello,

    I suggest you checking the "txtFile" is correct.

    Most probably, the file's path you pass to Workbook.Open is not valid.

    Regards,


    Damon Zheng
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, July 02, 2013 9:30 AM
  • I am sorry Kevin. It is a type. I have type C:\File.xlsx
    Wednesday, July 03, 2013 5:29 AM
  • Hi Damon, 

    The file seems to be fine. I am able to open it without much difficulty in all systems. The same code that I have given here works in a Windows 8 system but the error pops up in a XP system. That is what I am not able to comprehend.

    Thanks, 

    Wednesday, July 03, 2013 5:31 AM
  • 1.

    >excelConString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=File.xlsx"";Extended Properties='Excel 8.0;IMEX=1;'";

    Shouldn't this be "File.xls"

    2.

    Since you are using Office Automation, Could you please confirm if this system has xl2007+ installed?


    Sid

    Wednesday, July 03, 2013 6:26 AM
  • I am sorry Kevin. It is a type. I have type C:\File.xlsx

    Are you sure, see below from your original post. lsx does not match xls.

    if (Path.GetExtension("C:\xFile.lsx") == "xls")


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.

    Wednesday, July 03, 2013 7:19 AM
  • Yes Kevin. It was a typo. I typed it wrong in this thread.
    Friday, July 05, 2013 11:49 AM
  • Yes Siddharth. xl2007 is installed in the system.
    Friday, July 05, 2013 11:50 AM
  • Hi Devaks,

    Have you tried:

    if (Path.GetExtension("@C:\File.xls") == ".xls")
    {
    excelConString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=File.xlsx"";Extended Properties='Excel 8.0;IMEX=1;'";
    }
    else
    {
    excelConString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source="File.xlsx";Extended Properties='Excel 12.0;IMEX=1;'";
    }
    

    Friday, July 05, 2013 2:54 PM
  • Devaks-

    Take a look at this for some more information that should help you quite a bit.

    http://csharp.net-informations.com/excel/csharp-excel-tutorial.htm


    Knowledge is the only thing that I can give you, and still retain, and we are both better off for it.

    Monday, September 23, 2013 11:53 PM