[英]count number of rows in excel using c#
我有excel表,代碼如
VPUDB001
VPUDB002
VPUDB003
VPUDB004
VPUDB005
VPUDB006
VPUDB007
VPUDB008
VPUDB009
VPUDB010
VPUDB011
VPULN001
VPULN002
VPULN003
VPULN004
VPULN005
VPULN006
VPULN007
VPULN008
VPULN009
我想在excel中顯示以VPUDB
和VPULN
開頭的行數。
private void browse_Click(object sender, EventArgs e)
{
DialogResult fileopen = openFileDialog1.ShowDialog();
string filename = openFileDialog1.FileName;
textBox1.Text = filename;
try
{
olecon = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" + filename + "';Extended Properties=Excel 12.0");
olecon.Open();
DataTable dt = new DataTable();
dt = olecon.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
string[] excelsheetnames = new string[dt.Rows.Count];
int i = 0;
foreach (DataRow row in dt.Rows)
{
excelsheetnames[i] = row["TABLE_NAME"].ToString();
string sheetnamealone = excelsheetnames[i].Remove(excelsheetnames[i].IndexOf('$'));
comboBox1.Items.Add(sheetnamealone);
i++;
}
//comboBox1.SelectedIndex = 1;
}
catch
{
}
finally
{
olecon.Close();
}
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
OleDbCommand cmd = new OleDbCommand("Select [code] FROM ["+comboBox1.SelectedItem+"$] ", olecon);
olecon.Open();
OleDbDataReader dReader;
dReader = cmd.ExecuteReader();
while (dReader.Read())
{
string s = dReader[0].ToString();
}
olecon.Close();
}
在這里我瀏覽excel表,我計算並顯示在組合框中,然后在組合選擇的事件中我需要計算特定的表行如上所述。
在最初計算組合框時,使用字符串比較來查看它是否包含VPUDB
或VPULN
,如果是,則增加計數器。
嘗試這樣的linq將解決您的問題
var query = from row in dTable.AsEnumerable()
where ((row.Field<string>("columnname")).contains("VPUDB")
|| (row.Field<string>("columnname")).contains("VPULN"))
select row;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.