计数具有特定值的单元 – 线程通信

我需要知道特定列(列“C”)中具有特定值(“DE”或“CH”或“AT”)的行数

在这一刻,我正在使用一个for循环,这个工作,但问题是,我的程序需要近10分钟才能通过我的Excel表40k行

有没有办法让这个信息更快?

编辑:

我现在有这个问题,我有3个线程使用相同的Excelsheet,这是没有问题,当我等待“//MessageBox.Show("Es gibt”+ Convert.ToString(count)+“Verträgefür” + searchword +“!”);“ 对于所有线程,并在所有线程都准备就绪后单击确定。 当我不等它的时候,我从Excel中得到2个提示(下面的图片),Programm崩溃。

正如我所看到的,当第一个线程准备就绪时,它会closuresExcel表格和应用程序,其他线程有问题或什么? 当程序崩溃它说:“exception从HRESULT:0x800AC472”(评论)任何想法?

下一个问题是:要从线程获取信息到主窗体,我使用.txt文件,有没有什么好的方法来获取信息返回到主窗体或让线程相互沟通? 这个结论并不是很好:/

ExcelPrompt

class RowCheckThread { public RowCheckThread() { } public void asdf() { string localrow = row; string localsearchword = searchword; string localfile = file; Excel.Application ExcelApp = new Excel.Application(); Excel.Workbook ExcelWorkBook = ExcelApp.Workbooks.Open(localfile); Excel.Sheets ExcelSheets = ExcelWorkBook.Worksheets; Excel.Worksheet Sheet = (Excel.Worksheet)ExcelWorkBook.Worksheets.get_Item(1); Excel.Range allCellsInColumn = Sheet.get_Range(localrow + ":" + localrow); // Excel.Range usedCells = allCellsInColumn.Find(localsearchword, LookAt: Excel.XlLookAt.xlWhole, SearchOrder: Excel.XlSearchOrder.xlByRows, SearchDirection: Excel.XlSearchDirection.xlNext);//Sucht ersten wert mit searchwort string firstFound = usedCells.get_Address(); Excel.Range next = allCellsInColumn.FindNext(usedCells); string nextFound = next.get_Address(); int count = 1; while (nextFound != firstFound) { next = allCellsInColumn.FindNext(next);//Exception from HRESULT: 0x800AC472 nextFound = next.get_Address(); count++; if (KeepAlive == false) { ExcelWorkBook.Close(); ExcelApp.Quit(); return; } } //MessageBox.Show("Es gibt " + Convert.ToString(count) + " Verträge für " + searchword + "!"); ExcelWorkBook.Close(); ExcelApp.Quit(); string path = System.IO.Path.GetTempPath() + "test.txt"; int wert = 0; if(File.Exists(path)) { StreamReader myFile = new StreamReader(path, System.Text.Encoding.Default); wert = Convert.ToInt32(myFile.ReadToEnd()); myFile.Close(); } wert = wert + count; StreamWriter tempfile = new StreamWriter(path); tempfile.Write(wert); tempfile.Close(); } public string row { get; set; } public string searchword { get; set; } public string file { get; set; } public bool KeepAlive { get; set; } } 

如果你是自动化的Excel,不妨使用WorksheetFunction.CountIf

使用这个函数,你只要给出范围和匹配标准。

电话会看起来像这样:

 Application.WorksheetFunction.CountIf(yourRange, "DE"); 

如果这个花费了40,000个单元的时间超过几秒,我会感到惊讶。

function的文档在这里 。

Jochot尝试下面的代码,在第二列中search关键字“DE”

  Excel.Application ExcelApp = new Excel.Application(); Excel.Workbook ExcelWorkBook = ExcelApp.Workbooks.Open(@"E:\test.xlsx"); Excel.Sheets ExcelSheets = ExcelWorkBook.Worksheets; Excel.Worksheet Sheet = (Excel.Worksheet)ExcelSheets.get_Item("Sheet1"); Excel.Range allCellsInColumn = Sheet.get_Range("B:B"); Excel.Range usedCells = allCellsInColumn.Find("DE", LookAt: Excel.XlLookAt.xlWhole, SearchOrder: Excel.XlSearchOrder.xlByRows, SearchDirection: Excel.XlSearchDirection.xlNext); string firstFound = usedCells.get_Address(); Excel.Range next = allCellsInColumn.FindNext(usedCells); string nextFound = next.get_Address(); int count = 1; while (nextFound != firstFound) { next = allCellsInColumn.FindNext(next); nextFound = next.get_Address(); count++; } Console.WriteLine("Search Found in {0} Rows",count); ExcelWorkBook.Save(); ExcelWorkBook.Close(); ExcelApp.Quit();