【发布时间】:2013-10-15 07:48:07
【问题描述】:
我正在使用这些连接字符串,具体取决于文件的扩展名:
public static string ExcelConnectionString(string filePath)
{
OleDbConnectionStringBuilder sbConnection = new OleDbConnectionStringBuilder();
String strExtendedProperties = String.Empty;
sbConnection.DataSource = filePath;
if (Path.GetExtension(filePath).Equals(".xls"))//for 97-03 Excel file
{
sbConnection.Provider = "Microsoft.Jet.OLEDB.4.0";
strExtendedProperties = "Excel 8.0";//HDR=ColumnHeader,IMEX=InterMixed
}
else if (Path.GetExtension(filePath).Equals(".xlsx")) //for 2007 Excel file
{
sbConnection.Provider = "Microsoft.ACE.OLEDB.12.0";
strExtendedProperties = "Excel 12.0";
}
sbConnection.Add("Extended Properties", strExtendedProperties);
return sbConnection.ToString();
}
但是,当我在不使用“另存为:2003 xls”或“另存为:xlsx”选项的情况下导入 xls 文件时。 (因此,当我通过 control + s 保存下载的文件时,它将不起作用。)我最终得到了这个错误:
用户代码未处理 OleDbException - 外部表不是预期的格式。
我该如何解决这个问题?我的系统需要使用 control + s 来保存文件,而不是手动选择 2 个中的一个。
问题可能是我做的导出*,因为当我在导出后手动打开文件时,我看到了这个窗口:
The file you are trying to open "yourfile.xls" in a different format than specified file extension. Verify that the file is not corrupted and is from a trusted source before opening the file. Do you want to open the file now?
*导出文件是生成的 XML 文件,另存为 .xls。 有什么方法可以将其保存为 .xls 而不会发出警告?我真的需要 control + s 来保存文件。
提前致谢。
简而言之: 如何将我创建的 XML 文件保存为 XLS 而不会出现上面显示的错误。 我不想使用外部库,除非没有它们真的不可能做到这一点。
【问题讨论】:
-
您能提供一个示例文件吗?在您使用连接字符串打开文件的位置发布代码也可能会有所帮助。