【问题标题】:IExcelDataReader is reading xlsx but not xls filesIExcelDataReader 正在读取 xlsx 但不是 xls 文件
【发布时间】:2016-08-31 00:32:32
【问题描述】:

我是 Excel 数据阅读器的新手,但遇到了问题。 我的程序只读取 .xlsx 文件,无法读取 .xls 文件。 任何帮助将不胜感激 这是我的代码:

OpenFileDialog ope = new OpenFileDialog();
            ope.Filter = "Excel Files|*.xls;*.xlsx;*.xlsm";

            Invoke((Action)(() => { ope.ShowDialog(); }));
            label2.Invoke((MethodInvoker)delegate
            {
                label2.Visible = true;
            });

            FileStream stream = new FileStream(ope.FileName, FileMode.Open);
            IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
            DataSet result = excelReader.AsDataSet();

            string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["SQL"].ConnectionString;
            SqlConnection conna = new SqlConnection(connectionString);

            conna.Open();


            foreach (DataTable table in result.Tables)
            {
                foreach (DataRow dr in table.Rows)
                {
                    try
                    {

}
}
}
            excelReader.Close();
            stream.Close();

非常感谢!

打开xls文件时的调试:

The thread 0xf58 has exited with code 259 (0x103).
The thread 0x2764 has exited with code 259 (0x103).
'ProjectExcelv2.vshost.exe' (CLR v4.0.30319: ProjectExcelv2.vshost.exe): Loaded 'C:\Users\Marios\Desktop\C# Projects\eStatistics\ProjectExcelv2\ProjectExcelv2\bin\Debug\ProjectExcelv2.exe'. Symbols loaded.
'ProjectExcelv2.vshost.exe' (CLR v4.0.30319: ProjectExcelv2.vshost.exe): Loaded 'C:\Users\Marios\Desktop\C# Projects\eStatistics\ProjectExcelv2\ProjectExcelv2\bin\Debug\Excel.4.5.dll'. Symbols loaded.
'ProjectExcelv2.vshost.exe' (CLR v4.0.30319: ProjectExcelv2.vshost.exe): Loaded 'C:\Users\Marios\Desktop\C# Projects\eStatistics\ProjectExcelv2\ProjectExcelv2\bin\Debug\ICSharpCode.SharpZipLib.dll'. Module was built without symbols.
A first chance exception of type 'ICSharpCode.SharpZipLib.Zip.ZipException' occurred in ICSharpCode.SharpZipLib.dll
'ProjectExcelv2.vshost.exe' (CLR v4.0.30319: ProjectExcelv2.vshost.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_32\System.Transactions\v4.0_4.0.0.0__b77a5c561934e089\System.Transactions.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'ProjectExcelv2.vshost.exe' (CLR v4.0.30319: ProjectExcelv2.vshost.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_32\System.EnterpriseServices\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.EnterpriseServices.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'ProjectExcelv2.vshost.exe' (CLR v4.0.30319: ProjectExcelv2.vshost.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_32\System.EnterpriseServices\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.EnterpriseServices.Wrapper.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
A first chance exception of type 'System.NullReferenceException' occurred in ProjectExcelv2.exe

【问题讨论】:

  • 您是否看到任何错误消息?
  • 是的,结果对象为空,消息如下:ProjectExcelv2.exe 中发生“System.NullReferenceException”类型的异常,但未在用户代码中处理其他信息:对象引用未设置为对象的实例。
  • 请调试您的代码并确定System.NullReferenceException 出现的位置。

标签: c# excel dataset filestream openfiledialog


【解决方案1】:

对于 .xls 文件,请尝试使用:

ExcelReaderFactory.CreateBinaryReader(stream)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-27
    • 1970-01-01
    • 2015-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-08
    • 1970-01-01
    相关资源
    最近更新 更多