优化Excel文件处理器

时间:2018-09-07 09:01:46

标签: c# excel optimization

我们有一个程序可以转换excel文件中的列,因为我们必须先处理产品提供商的RSS提要,然后才能将eproduct列表发布到网站上。 但是,Feed中有32,000行需要更新,并且需要10分钟以上才能完成所有操作。 我不确定如何优化此代码,它已在异步中的后台线程上运行 https://pastebin.com/ukeQ745G

    private void btnSortGroupedProducts_Click(object sender, EventArgs e)
{
    //Closes any existing excel processes
    closeProcess();

    //Creates the objects needed
    Excel.Application excelApp;
    excelApp = new Excel.Application();
    excelApp.DisplayAlerts = false;

    //Imports the user's file
    Excel.Workbook workBook = excelApp.Workbooks.Open(openFileDialog1.FileName,
    Type.Missing, Type.Missing, Type.Missing, Type.Missing,
    Type.Missing, Type.Missing, Type.Missing, Type.Missing,
    Type.Missing, Type.Missing, Type.Missing, Type.Missing,
    Type.Missing, Type.Missing);

    //Gets total number of sheets
    int numSheets = workBook.Sheets.Count;

    //Opens the first sheet ONLY
    Excel.Worksheet workSheet = workBook.Sheets[1];
    int totalRowNumber = workSheet.UsedRange.Rows.Count;
    //pbFixImages.Maximum = totalRowNumber * 2;

    //Adds needed columns with headers
    Excel.Range type = workSheet.Rows.Cells[1, 48];
    Excel.Range grouped_skus = workSheet.Rows.Cells[1, 49];
    type.Value = "type";
    grouped_skus.Value = "grouped_skus";

    //Opens needed cells
    Excel.Range productNames = workSheet.get_Range("N2:N" + totalRowNumber);
    int rowNumber = 2;
    int rowCountSave = 0;
    string currentName = "";
    string previousName = "This is a test";
    string likePrevious = "";
    string groupedSkuString = "";
    Excel.Range previousSku = workSheet.Rows.Cells[1, 6];
    Excel.Range currentSku = workSheet.Rows.Cells[1, 6];
    Excel.Range newSku = workSheet.Rows.Cells[rowNumber, 6];
    bool lastInGroup = false;
    bool previousIsGrouped = false;
    bool valid = true;
    int groupedProdChanges = 0;

    //Progress Bars
    pbStage.Invoke(new Action(() => pbStage.Maximum = totalRowNumber));
    pbStage.Invoke(new Action(() => pbStage.Minimum = 1));
    pbStage.Invoke(new Action(() => pbStage.Value = 1));

    //Goes through each cell needed and performs nessarsary actions
    foreach (Excel.Range item in productNames.Cells)
    {
        txtStatus.Invoke(new Action(() => txtStatus.Text = "Stage Two: Working on Row #" + rowNumber));
        try
        {
            if (rowNumber == 2)
            {
                currentName = item.Value;
            }
            else
            {
                previousName = currentName;
                currentName = item.Value.ToString();
            }
        }
        catch (Exception ex)
        {
            logError("Error 6: " + ex.Message);
        }
        try
        {
            likePrevious = previousName.Substring(0, 5);
        }
        catch (Exception ex)
        {
            logError("Error 7: " + ex.Message);
        }

        if (currentName.Contains(likePrevious.ToString()))
        {
            //Set type to simple
            type = workSheet.Rows.Cells[rowNumber, 48];
            type.Value = "simple";

            //Add to groupedSkuString
            previousSku = workSheet.Rows.Cells[(rowNumber - 1), 6];
            currentSku = workSheet.Rows.Cells[rowNumber, 6];
            groupedSkuString = groupedSkuString + currentSku.Value + ",";
            string test = previousSku.Value.ToString();
            try
            {
                if (groupedSkuString.Contains(test.ToString()) == false)
                {
                    groupedSkuString = groupedSkuString + previousSku.Value + ",";
                }
            }
            catch (Exception ex)
            {
                logError("Error 5: " + ex.Message);
            }

            //Set previousIsGrouped = True
            previousIsGrouped = true;
        }

        else
        {
            if (previousIsGrouped == true)
            {
                lastInGroup = true;
                previousIsGrouped = false;
                //Set type = simple:
                type = workSheet.Rows.Cells[rowNumber, 48];
                type.Value = "simple";
            }
        }

        if (lastInGroup == true)
        {
            try
            {
                int totalRowNumber2 = workSheet.UsedRange.Rows.Count;
                //Make new row
                Excel.Range source = workSheet.Range["A" + (rowNumber - 1) + ":AW" + (rowNumber - 1)];
                Excel.Range dest = workSheet.Range["A" + (totalRowNumber2 + 1)];
                source.Copy(dest);
                //Add grouped skus to row
                totalRowNumber2 = workSheet.UsedRange.Rows.Count; //needed to re-count the rows and add to the right row.
                groupedSkuString = groupedSkuString.Remove(groupedSkuString.Length - 1);
                grouped_skus = workSheet.Rows.Cells[totalRowNumber2, 49];
                grouped_skus.Value = groupedSkuString;
                //Set type = grouped
                type = workSheet.Rows.Cells[totalRowNumber2, 48];
                type.Value = "grouped";
                //Modify the sku so it isn't the same as another sku
                newSku = workSheet.Rows.Cells[totalRowNumber2, 6];
                newSku.Value = newSku.Value.Remove(newSku.Value.Length - 2);
                //Reset groupedSkuString
                groupedSkuString = "";
                //Set lastInGroup = false
                lastInGroup = false;
                groupedProdChanges += 1;
            }
            catch (Exception ex)
            {
                logError("Error 4: " + ex.Message);
            }
        }
        else
        {
            try
            {
                //Set type to simple
                type = workSheet.Rows.Cells[rowNumber, 48];
                type.Value = "simple";
            }
            catch (Exception ex)
            {
                logError("Error 3: " + ex.Message);
            }
        }
        if (rowCountSave == 1000)
        {
            try
            {
                rowCountSave = 0;
                workBook.SaveAs(openFileDialog1.FileName, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookDefault, Type.Missing, Type.Missing,
                false, false, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange,
                Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
            }
            catch (Exception ex)
            {
                logError("Error 2: " + ex.Message);
            }
        }
        rowNumber += 1;
        pbStage.Invoke(new Action(() => pbStage.PerformStep()));
        pbOverall.Invoke(new Action(() => pbOverall.PerformStep()));
        rowCountSave += 1;
    }
    try
    {
        //Save and close the file
        workBook.SaveAs(openFileDialog1.FileName, Excel.XlFileFormat.xlWorkbookDefault);
    }
    catch (Exception ex)
    {
        logError("Error 1: " + ex.Message);
        valid = false;
        closeProcess();
        frmShowErrors FormTwo = new frmShowErrors();
        FormTwo.Show();
    }
    if (valid == true)
    {
        MessageBox.Show("Done, total product changes: ", groupedProdChanges.ToString());
        closeProcess();

    }

}

0 个答案:

没有答案