我是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文件时的Debug:
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
答案 0 :(得分:0)
对于.xls文件,请尝试使用:
ExcelReaderFactory.CreateBinaryReader(stream)