reading excel 2003(.xls) in c#
i have an issue in reading excel 2003 format file (xls).
i am using vs2008 with windows 7. and 64 bit Processor
the below code works fine if i send an excel 2007 format file (.xlsx) i have office 2007开发者_Go百科 been installed in my system.
here i have add refrence
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
the above code works fine in system for excel 2003(.xls) where i am using Xp,VS 2008.
the code works fine where i am able to read excel 2003 data with no issue.
but the same code fails in windows 7, 64 bit proocessor
any help how i can solve the problem would be great.
thanks prince
The ACE provider is 32-bit and your application should be compiled in 32-bit mode to use it. Here's a thread that might be helpful.
Use Excel Data Reader for reading Excel file in applications.
http://exceldatareader.codeplex.com/
精彩评论