Problems reading in an Excel file in C#

后端 未结 4 2076
不思量自难忘°
不思量自难忘° 2021-01-23 07:57

I\'m reading an Excel file with OLDB Connection using this code

        var connectionString = string.Format(\"Provider=Microsoft.Jet.OLEDB.4.0; data source={0};         


        
相关标签:
4条回答
  • 2021-01-23 08:12

    Just make sure that your excel file is not open. Close your excel application & then start your program.

    Sunil

    0 讨论(0)
  • 2021-01-23 08:13

    SpreadsheetGear for .NET will let you load Excel workbooks from C# and access the underlying cell values or the formatted values in any order no matter how the workbook is laid out or what the types of the cells are.

    You can see live ASP.NET samples here and download the free trial here if you want to try it yourself.

    Disclaimer: I own SpreadsheetGear LLC

    0 讨论(0)
  • 2021-01-23 08:32

    I had this exact problem and solve it with using IMEX setting. In case others are wondering how to include the IMEX, here is what I have on my connection string

    string connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties='Excel 8.0;IMEX=1';Data Source={0};";
    connectionString = string.Format(connectionString, excelWorkbookPath);
    
    0 讨论(0)
  • 2021-01-23 08:34

    Check the first examples here: http://www.connectionstrings.com/excel

    What often goes wrong is that Excel will estimate the type of a column based upon the first X rows. When after that the values don't match, these rows get empty values. I'm afraid that going into the registry is sometime the only way to get the Excel driver to scan all rows first (as described in the connectionstrings.com article).

    Play around with the HDR and IMEX settings in your environment. In some cases that will help as well.

    0 讨论(0)
提交回复
热议问题