我收到错误“System.Data.OleDb.OleDbException:外部表格不是预期的格式。”

时间:2017-11-23 06:57:48

标签: c# excel

当我首先尝试阅读.xls.xlsx秒时,我收到此错误。 我正在使用的连接字符串是:

public string GetConnectionString(string DataFile)
{
    Dictionary<string, string> props = new Dictionary<string, string>();

    string extension = System.IO.Path.GetExtension(DataFile);

    props["Provider"] = "Microsoft.ACE.OLEDB.12.0;";
    if (extension == ".xlsx")
        props["Extended Properties"] = "Excel 12.0;";
    else
        props["Extended Properties"] = "Excel 8.0;";
    props["Data Source"] = DataFile;

    StringBuilder sb = new StringBuilder();
    foreach (KeyValuePair<string, string> prop in props)
    {
        sb.Append(prop.Key);
        sb.Append('=');
        sb.Append(prop.Value);
        sb.Append(';');
    }
    return sb.ToString();
}

有人可以帮我解决这个问题吗?

0 个答案:

没有答案