【问题标题】:reading excel 2003(.xls) in c#在 C# 中读取 excel 2003(.xls)
【发布时间】:2010-08-16 08:56:27
【问题描述】:

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

我正在使用带有 windows 7. 和 64 位处理器的 vs2008

如果我发送 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

以上代码在我使用 Xp、VS 2008 的 excel 2003(.xls) 系统中运行良好。

代码工作正常,我可以毫无问题地读取 excel 2003 数据。

但相同的代码在 Windows 7 64 位处理器中失败

任何帮助我如何解决问题都会很棒。

谢谢 王子

【问题讨论】:

    标签: c# excel


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      使用 Excel Data Reader 在应用程序中读取 Excel 文件。

      http://exceldatareader.codeplex.com/

      【讨论】:

      • Koogra 非常适合阅读 excel。它不需要安装办公室。成功使用它。
      • 大家都说用exceldatareader,用koogra。这个很酷!但我可以找到任何样本或一些体面的文档。你们可以发布一个带有很好示例的链接吗?我无法使用 exceldatareader 从 excel 中获取 DateTime 值。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多