我正在使用Microsoft.ACE.OLEDB.12.0连接到Microsoft Excel文件并从中获取数据。我使用Visual Studio 2012在C#语言中编写代码。 这是我的代码:
public DataTable getData(string fileName, string sheetName)
{
connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source='"
+ fileName
+ "';Extended Properties= 'Excel 8.0;HDR=Yes;IMEX=1'";
errorCode = ErrorDefinition.ERROR_NOERROR;
errorMessage = "";
DataTable dt = new DataTable();
try
{
string query = "SELECT * FROM [" + sheetName + "]";
OleDbConnection con = new OleDbConnection(connectionString);
OleDbDataAdapter dataAdapter = new OleDbDataAdapter(query, con);
dataAdapter.Fill(dt);
}
catch (Exception exp)
{
errorCode = ErrorDefinition.ERROR_OLEDBERROR;
errorMessage = exp.Message;
}
return dt;
}
问题在于dt
中不包含文件中指定表格的第一行。这是什么问题?