我得到Excel表格名稱,但只是有數據的表格。如何使用oledb c#知道哪些excel表單爲空?
String fpath = "Provider=Microsoft.ACE.OLEDB.12.0; data source=" +tbpath.Text+ ";Extended Properties='Excel 12.0 Xml;HDR=YES';";
file = new OleDbConnection(fpath);
file.Open();
dt = file.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
if (dt == null)
{
//return null;
}
cbsheet.Enabled = true;
//String[] excelSheets = new String[dt.Rows.Count];
//int i = 0;
// Add the sheet name to the string array.
foreach (DataRow row in dt.Rows)
{
if (row["TABLE_NAME"].ToString().Contains("$"))//checks whether row contains '_xlnm#_FilterDatabase' or sheet name(i.e. sheet name always ends with $ sign)
{
cbsheet.Items.Add(row["TABLE_NAME"].ToString());
}
}
//return excelSheets;
}
catch (Exception ex)
{
MessageBox.Show("ERROR: "+ex);
}
我複製了你的代碼。我只獲得了包含數據的表單。我重新命名了第二張紙,現在即使它不包含任何數據,我仍然可以獲得該紙張 –
我只需要使用數據表。 – user2901626
您是否需要代碼建議的表單或表單名稱中的數據? –