C# Excel C中缺少第一列和第一行
我试图在excel中读取excel文件,但由于某些原因,数据中缺少第一列和第一行 当我在excel中打开文件并保存它而不做任何更改时,文件被正确读取 你知道这是怎么发生的吗 下面是我用来读取文件的代码:C# Excel C中缺少第一列和第一行,c#,excel,excel-2007,C#,Excel,Excel 2007,我试图在excel中读取excel文件,但由于某些原因,数据中缺少第一列和第一行 当我在excel中打开文件并保存它而不做任何更改时,文件被正确读取 你知道这是怎么发生的吗 下面是我用来读取文件的代码: string xlConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + txt_InputFile.Text + ";Extended Propertie
string xlConn = "Provider=Microsoft.Jet.OLEDB.4.0;"
+ "Data Source="
+ txt_InputFile.Text
+ ";Extended Properties=Excel 8.0;";
using (OleDbConnection dbConnection = new OleDbConnection(xlConn))
{
dbConnection.Open();
// Get the name of the first worksheet:
DataTable dbSchema = dbConnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
if (dbSchema == null || dbSchema.Rows.Count < 1)
{
//"Error: Could not determine the name of the first worksheet."
throw new Exception(Program.lm_GetMethodLanguage(this.GetType().Name, "wp_InputFile_CloseFromNext", 5) );
}
string firstSheetName = dbSchema.Rows[0]["TABLE_NAME"].ToString();
using (
OleDbDataAdapter dbCommand = new OleDbDataAdapter("SELECT * FROM [" + firstSheetName + "]",
dbConnection))
{
using (DataSet myDataSet = new DataSet())
{
dbCommand.Fill(myDataSet);
inputData = myDataSet.Tables[0];
}
}
}
使用此选项。这将检索excel工作表中的所有工作表
private String[] GetExcelSheetNames(string excelFile)
{
try
{
excelConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + ""yoursourcepath"+ ";Extended Properties=Excel 12.0;Persist Security Info=False";
excelConnection = new OleDbConnection(excelConnectionString);
excelConnection.Open();
dt = excelConnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
if (dt == null)
{
return null;
}
excelSheets = new String[dt.Rows.Count];
int i = 0;
foreach (DataRow row in dt.Rows)
{
excelSheets[i] = row["TABLE_NAME"].ToString();
i++;
}
return excelSheets;
}
catch (Exception ex)
{
return null;
}
finally
{
if (excelConnection != null)
{
excelConnection.Close();
excelConnection.Dispose();
}
if (dt != null)
{
dt.Dispose();
}
}
}
尝试在连接字符串中设置HDR=No,可能吗?我尝试将HRD=No添加到连接字符串中,但出现以下异常:找不到可安装的ISAM。我只是通过添加引号修复了此错误,但是缺少第一列和第二行的问题需要注意的是,GetOleDbSchemaTable将按字母顺序检索工作表名称,而不是它们在Excel中的显示顺序。现在确定这对你的情况有没有影响。奇怪。那么你的电子表格肯定有什么不寻常的地方。首先是什么产生的?@AhmadHajou不,这只适用于2007年及以后。此外,这将仅检索图纸名称,而不是图纸数据。