C# 关闭Excel文件时,外部表不是预期格式
我的导入可以很好地处理.xls文件,但它不能处理我在Excel 2010中创建的.xlsx文件,除非它已打开 我的代码如下所示:C# 关闭Excel文件时,外部表不是预期格式,c#,oledb,import-from-excel,C#,Oledb,Import From Excel,我的导入可以很好地处理.xls文件,但它不能处理我在Excel 2010中创建的.xlsx文件,除非它已打开 我的代码如下所示: public static DataSet Sheets(string filePath, bool header) { DataSet dsResults = new DataSet(); string hasHeader = header ? "YES" : "NO"; Ol
public static DataSet Sheets(string filePath, bool header)
{
DataSet dsResults = new DataSet();
string hasHeader = header ? "YES" : "NO";
OleDbConnectionStringBuilder sbConnection = new OleDbConnectionStringBuilder();
String strExtendedProperties = String.Empty;
sbConnection.DataSource = filePath;
if (Path.GetExtension(filePath).ToLower().Equals(".xls"))//Excel 97-03
{
sbConnection.Provider = "Microsoft.Jet.OLEDB.4.0";
strExtendedProperties = String.Format("Excel 8.0;HDR={0};IMEX=1", header);
}
else if (Path.GetExtension(filePath).ToLower().Equals(".xlsx")) // Excel 2007
{
sbConnection.Provider = "Microsoft.ACE.OLEDB.12.0";
strExtendedProperties = String.Format("Excel 12.0;HDR={0};IMEX=1", header);
}
sbConnection.Add("Extended Properties", strExtendedProperties);
using (OleDbConnection conn = new OleDbConnection(sbConnection.ToString()))
{
conn.Open();
DataTable dtSheet = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
foreach (DataRow drSheet in dtSheet.Rows)
{
if (drSheet["TABLE_NAME"].ToString().Contains("$") && !drSheet["TABLE_NAME"].ToString().EndsWith("_"))
{ .......
我在conn.Open()上收到的错误代码>
我也看到过一些解决方案,说要改变其中的一些东西,然后以其他方式再次保存它。问题是我必须让它在不这样做的情况下工作,因为它进入了一个DLL,该DLL将提供给其他开发者,他们将使用它在网站上上传文件。因此,用户(客户端)将上载一个文件,当验证表明它是有效的excel文件时,它将进入我的代码。请说明错误。@Yogendra您是指我在标题中写的内容吗?好:“外部表格不是预期的格式”@bokkie:)我特别喜欢这张表格的讽刺意味@巴特;)我也是你的编辑:))