我正在尝试在我的 windows 窗体 gui 内的进度条的一部分中获取 excel 列中的唯一条目总数。然而,当我调用下面的函数时,它只返回一行。我正在使用 worksheet.rows.count 来查找唯一的“程序 ID”条目总数。
public static int procedureTotals(string excelFile) {
string connection = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + excelFile
+ ";Extended Properties='Excel 12.0;HDR=YES;'";
string sql = null;
string worksheetName = null;
DataTable schema = null;
DataTable worksheet = null;
DataSet workbook = new DataSet();
OleDbConnection objconn = new OleDbConnection(connection);
objconn.Open();
schema = objconn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
worksheetName = schema.Rows[0]["Table_Name"].ToString();
sql = "SELECT DISTINCT 'Procedure ID' FROM[" + worksheetName + "]";// GROUP BY 'Procedure ID'";
//data adapter
OleDbDataAdapter objAdapter = new OleDbDataAdapter();
objAdapter.SelectCommand = new OleDbCommand(sql, objconn);
objAdapter.Fill(workbook);
worksheet = workbook.Tables[0];
objconn.Close();
return worksheet.Rows.Count;
}