我需要上传excel文件,阅读其内容并将其插入我的数据库。
我总是收到这个错误:
初始化字符串的格式不符合规范 从索引186开始。
我找到了一些解决方案(尽管大多数都使用C#
),我试图应用它们但没有成功:
connString = ("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & path & ";Extended Properties=""Excel 12.0;HDR=NO;"")")
我也尝试过:
connString = ("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & path & ";Extended Properties=Excel 12.0;)")
但它们都不起作用。这是路径:
"C:\Users\tama\Documents\Visual Studio 2008\Projects\uploader\uploader\File\Plan TEST.xlsx"
我确保path
正确且文件格式为xlsx
,因此我使用版本12.0
。
答案 0 :(得分:1)
var pathExtension = Path.GetExtension(fileName);
var connectionString = string.Empty;
if (pathExtension == ".xls")
{
connString = string.Format(@"PProvider=Microsoft.ACE.OLEDB.12.0;Data Source={0}; Extended Properties=\"Excel 8.0;HDR=YES\";", yourPath)
}
if (pathExtension == ".xlsx")
{
connString = string.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"Excel 12.0 Xml;HDR=YES\";", yourPath)
}