我正在获取 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);
}