gpt4 book ai didi

c# - 外部表不是预期的格式。

转载 作者:行者123 更新时间:2023-11-30 16:08:38 24 4
gpt4 key购买 nike

我正在创建一个小应用程序来将 excel 数据导入我的数据库,当我单击该按钮时它因错误而崩溃

External table is not in the expected format.

我尝试在谷歌上搜索并更改代码,但问题仍然存在。我尝试将文件另存为 .xls,当我运行代码时,页面因 google chrome's This webpage is not available(甚至无法进入调试)而脱机

这是我的代码:

    string strConnection = ConfigurationManager.ConnectionStrings["---"].ConnectionString;

String excelConnString = String.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"Excel 12.0\"", filePath);
//Create Connection to Excel work book
using (OleDbConnection excelConnection = new OleDbConnection(excelConnString))
{
//Create OleDbCommand to fetch data from Excel
using (OleDbCommand cmd = new OleDbCommand("Select * from [Sheet1$]", excelConnection))
{
excelConnection.Open();
using (OleDbDataReader dReader = cmd.ExecuteReader())
{
using (SqlBulkCopy sqlBulk = new SqlBulkCopy(strConnection))
{
//Give your Destination table name
sqlBulk.DestinationTableName = "TableName";
sqlBulk.WriteToServer(dReader);
}
}
}
}

最佳答案

假设您已完成所有准备工作,包括安装 Microsoft Access 2010 Runtime 要获取相关的驱动程序,请尝试以下方法,因为我过去曾成功使用它。

如果您的输入文件是 *.xlsx 类型,请尝试替换

Extended Properties="Excel 12.0"

在你的连接字符串中

Extended Properties="Excel 12.0 Xml"

此问题通常发生在 Excel 文件有问题或格式不匹配时,因此根据经验,您首先要尝试的是打开 Excel 并创建一个新的 .xlsx 文件其中包含您自己键入(而不是复制/粘贴)的示例数据。您不需要太多的输入来测试您的代码片段。几个具有您期望的正确信息的单元格就可以了。这种方法消除了实际 Excel 文件而不是 C# 代码的任何问题。

让我们知道你过得怎么样。

关于c# - 外部表不是预期的格式。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28923158/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com