【问题标题】:Two programs trying to read excel one succeeds one fails with same code两个程序试图读取 excel 一个成功一个失败,代码相同
【发布时间】:2016-06-09 16:03:11
【问题描述】:

我有两个彼此不相关的程序,但它们具有相同的方法来尝试从 excel 文件中读取行。

程序 1

    public DataTable GetExcelInfo(string filepath)
    {
        DataTable datatab = new DataTable();
        try
        {
            string connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filepath + ";Excel 8.0;IMEX=1;HDR=NO;TypeGuessRows=1;ImportMixedTypes=Text\\";
            using (OleDbConnection conn = new OleDbConnection(connectionString))
            {
                conn.Open();
                OleDbCommand cmd = new OleDbCommand("SELECT Format([F1], \"#\"), Format([F2], \"#\"), Format([F3], \"#\") FROM [Sheet1$]", conn);
                OleDbDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    string[] values = new string[3];                        
                    values[0] = reader.GetString(0);
                    values[1] = reader.GetString(1);
                    values[2] = reader.GetString(2);

                    DataRow dr = datatab.NewRow();
                    dr.ItemArray = values;
                    datatab.Rows.InsertAt(dr, i);
                    i++;
                }
            }
        }
    }

程序 2

private static DataTable GetInvoiceItems(string filepath)
{
        DataTable dt = new DataTable();
        string excelConString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filepath + ";Excel=8.0;IMEX=1;HDR=NO;TypeGuessRows=1;ImportMixedTypes=Text\\";
        using (OleDbConnection conn = new OleDbConnection(excelConString))
        {
            OleDbDataAdapter ada = new OleDbDataAdapter("SELECT [F1], [F2], [F3], [F4], [F5] FROM [Sheet1$]", conn);
            conn.Open();
            ada.Fill(dt);
        }
        return dt;
}

现在奇怪的是第一个程序可以在同一台电脑上完美运行,而第二个程序收到一个错误,说电脑上没有可安装的 ISAM。有什么建议吗?

【问题讨论】:

  • 您的 bin 目录中是否有任何 DLL?可能有一个名字以Ms开头的东西,比如Msexcl##.dll,或者Mspdbe##.dll,如果一个有另一个没有,试着把它复制过来。

标签: c# excel winforms aceoledb


【解决方案1】:

尽管您断言它们都是相同的,但如果您仔细观察它们却不是。看看你的连接字符串...

程序 1

"... Excel 8.0;IMEX=1;HDR=NO;TypeGuessRows=1;ImportMixedTypes=Text\\"

程序 2

"... Excel=8.0;IMEX=1;HDR=NO;TypeGuessRows=1;ImportMixedTypes=Text\\"

程序 2 中的 Excel 8.0 中不应有 =

【讨论】:

  • 我现在有点傻......非常感谢你指出我完全错过了......
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-17
  • 2020-09-05
  • 2015-04-18
  • 1970-01-01
相关资源
最近更新 更多