如何加快在Excel中删除列的速度?

时间:2018-08-28 05:51:39

标签: c# excel backgroundworker

我想加快在Excel中删除列的过程。我有2个Excel文件。假设存在所有数据的数据文件和另一个文件中的删除列名称。数据文件中有许多工作表,其中有些工作表超过15000列,而删除列文件中则有大约2500行。删除需要更长的时间。我的问题是如何加快下面的代码?

 private void workerdelete_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
        {
            try
            {
                totalRow = xlWorksheetdelvariable.UsedRange.Rows.Count;
                Excel.Range startCell = xlWorksheetdelvariable.Cells[2, 1];
                Excel.Range endCell = xlWorksheetdelvariable.Cells[totalRow, 1];
                Excel.Range currentRange = xlWorksheetdelvariable.get_Range(startCell, endCell).Cells;
                object[,] matrixReaddelvariable = (object[,])currentRange.Value;

                foreach (Excel.Worksheet sheet in xlWorkbookDelete.Sheets)
                {
                    xlWorksheetdelete = xlWorkbookDelete.Sheets[sheet.Index];
                    columnCount = xlWorksheetdelete.UsedRange.Columns.Count;
                    int rowCount = xlWorksheetdelete.UsedRange.Rows.Count;
                    Excel.Range startCelldelete = xlWorksheetdelete.Cells[1, 1];
                    Excel.Range endCelldelete = xlWorksheetdelete.Cells[1, columnCount];
                    Excel.Range currentRangedelete = xlWorksheetdelete.get_Range(startCelldelete, endCelldelete).Cells;
                    object[,] matrixReaddelete = (object[,])currentRangedelete.Value;
                    List<int> delCols = new List<int>();


                    for (int c = 1; c <= columnCount; c++)
                    {
                        for (int r = 1; r < totalRow; r++)
                        {
                            if (dellabel == true)
                            {
                                if (matrixReaddelete[1, c].ToString() == matrixReaddelvariable[r, 1].ToString()||matrixReaddelete[1, c].ToString().Contains("_label"))
                                {
                                    delCols.Insert(0, c);
                                    break;
                                }
                            }
                            else if(matrixReaddelete[1, c].ToString() == matrixReaddelvariable[r, 1].ToString())
                            {
                                delCols.Insert(0, c);
                                break;
                            }

                        }

                        int percentage = (c + 1) * 50 / columnCount;
                        workerdelete.ReportProgress(percentage);
                    }


                    foreach (int colIndex in delCols)
                    {
                        xlWorksheetdelete.Columns[colIndex].Delete();

                        int percentage = 50 + ( delCols.IndexOf(colIndex) + 1 ) * 50 / delCols.Count;
                        workerdelete.ReportProgress(percentage);
                    }
                }


            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show(ex.Message);
            }

        }   

0 个答案:

没有答案