Beispiel #1
0
        private void bW1_DoWork(object sender, DoWorkEventArgs e)
        {
            xlApp.Visible = false;
            string[] files = e.Argument as string[];
            var      num   = files.Count();

            numberOfFiles.Text = num.ToString();
            int percent = 0;

            rowcountFinal = 0;
            foreach (string fileEntrie in files)
            {
                Excel.Workbook  wb = xlApp.Workbooks.Open(fileEntrie);
                Excel.Worksheet ws = (Excel.Worksheet)wb.Worksheets[1];
                range = ws.UsedRange;


                //-2146827864


                percent++;
                //checkboxes status check
                if (atIM.Checked && chkdOut.Checked)
                {
                    range.AutoFilter(28, criteria1, Excel.Enums.XlAutoFilterOperator.xlOr, criteria2, true);
                }
                else if (atIM.Checked && !chkdOut.Checked)
                {
                    range.AutoFilter(28, criteria1);
                }
                else if (!atIM.Checked && chkdOut.Checked)
                {
                    range.AutoFilter(28, criteria2);
                }
                //---------------------------------------

                var visrange = range.SpecialCells(Excel.Enums.XlCellType.xlCellTypeVisible, Type.Missing);
                var rowcount = visrange.Count / visrange.Columns.Count;

                if (eHead.Checked)
                {
                    rowcount = rowcount - 1;
                }
                rowcountFinal = rowcountFinal + rowcount;
                string text = rowcountFinal.ToString();
                if (numberOfLines.InvokeRequired)
                {
                    SetTextCallback d = new SetTextCallback(FormUpdate);
                    Invoke(d, new object[] { text });
                }
                else
                {
                    // It's on the same thread, no need for Invoke
                    numberOfLines.Text = text;
                }


                if (bW1.CancellationPending)
                {
                    e.Cancel = true;
                    wb.Close(false);
                    xlApp.Application.Quit();
                    xlApp.Application.Dispose();
                    break;
                }
                double i = ((double)percent / num) * 100;
                int    currentPercentage = (int)i;
                bW1.ReportProgress(currentPercentage);
                wb.Close(false);
            }
        }