阅读Excel 2003(.xls)在C#

我在阅读excel 2003格式文件(xls)时遇到问题。

我用vs2008与Windows 7和64位处理器

下面的代码工作正常,如果我发送一个Excel 2007格式文件(.xlsx)我已经在我的系统中安装了Office 2007。

这里我添加引用

Microsoft Office 12.0 Object Library Microsoft.Office.Tools.Excel.dll Microsoft.Office.Tools.Excel.v9.0.dll 
 #region ExcelToDataTableChild /// <summary> /// Excel To DataTable for attendance employee /// </summary> /// <param name="strfilelocation"></param> /// <returns></returns> public DataTable ExcelToDataTableChild(string strfilelocation, string strfilename) { OleDbConnection excelConn = new OleDbConnection(); DataTable dtPatterns = new DataTable(); string excelConnStr = string.Empty; try { string fileName = Path.GetExtension(@strfilelocation); DataSet ds = new DataSet(); OleDbCommand excelCommand = new OleDbCommand(); OleDbDataAdapter excelDataAdapter = new OleDbDataAdapter(); #region Check FileExtension if (fileName.Equals(".xlsx")) { excelConnStr = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" + strfilelocation + "; Extended Properties =Excel 8.0;"; } if (fileName.Equals(".xls")) { excelConnStr = @"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + strfilelocation + "; Extended Properties=Excel 8.0;"; } #endregion excelConn = new OleDbConnection(excelConnStr); excelConn.Open(); //excelCommand = new OleDbCommand("SELECT `Employee Name` as PATTERN FROM [sheet1$]", excelConn); excelCommand = new OleDbCommand("SELECT * FROM [sheet1$]", excelConn); excelDataAdapter.SelectCommand = excelCommand; excelDataAdapter.Fill(dtPatterns); dtPatterns.TableName = "EmpList"; ds.Tables.Add(dtPatterns); dtPatterns.AcceptChanges(); } catch (Exception ex) { WriteLogError(ex.Message); } finally { excelConn.Close(); } return dtPatterns; } #endregion 

上面的代码在Excel 2003(.xls)系统中正常工作,我使用Xp,VS 2008。

代码工作正常,我能够读取Excel 2003的数据没有问题。

但相同的代码在Windows 7,64位proocessor失败

任何帮助我怎么能解决这个问题将是伟大的。

谢谢王子

ACE提供程序是32位,您的应用程序应该编译为32位模式才能使用它。 这是一个可能有用的线程 。

使用Excel数据读取器读取应用程序中的Excel文件。

http://exceldatareader.codeplex.com/