C#控制台应用程序速度太慢。有什么办法可以加快速度吗?

时间:2019-11-26 12:53:57

标签: c# excel console-application

我目前正在开发一个小程序,该程序通过excel文件并通过电子邮件将事情升级,并在excel的“进度条”中显示资料的进度。

程序运行正常,可以完成它打算做的所有事情,但是速度太慢。 大约需要11行,大约需要3分钟。

该文件最终将有越来越多的行(耦合在一起),并且将永远需要更新。

你们知道有什么方法可以加快速度吗?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Excel = Microsoft.Office.Interop.Excel;
using Microsoft.VisualStudio.Tools.Applications.Runtime;
using System.IO;
using Outlook = Microsoft.Office.Interop.Outlook;

namespace Prod_Tracking
{
    class Program
    {
        static void Main(string[] args)
        {

            Outlook.Application app = new Outlook.Application();
            Outlook.MailItem mailItem = app.CreateItem(Outlook.OlItemType.olMailItem);

            string rootPath = Directory.GetCurrentDirectory();
            string path = rootPath + "\\ProdTracking.xlsx";

            string email1 = "email@email.com";
            string email2 = "email@email.com";
            string body;
            string subject;
            Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
            Microsoft.Office.Interop.Excel.Workbook wb = excel.Workbooks.Open(path);
            Microsoft.Office.Interop.Excel.Worksheet tracking = excel.Sheets["Tracking"] as Microsoft.Office.Interop.Excel.Worksheet;
            Microsoft.Office.Interop.Excel.Worksheet coois = excel.Sheets["COOIS"] as Microsoft.Office.Interop.Excel.Worksheet;
            Microsoft.Office.Interop.Excel.Worksheet line = excel.Sheets["Line"] as Microsoft.Office.Interop.Excel.Worksheet;
            Microsoft.Office.Interop.Excel.Worksheet stat = excel.Sheets["Stat"] as Microsoft.Office.Interop.Excel.Worksheet;


            Excel.Range trackingRange = tracking.UsedRange;
            int mainRows = trackingRange.Rows.Count; //row number of main sheet

            coois.Select(true);
            Excel.Range cooisRange = coois.UsedRange;
            int cooisRows =cooisRange.Rows.Count; //row number of coois sheet

            line.Select(true);
            Excel.Range lineRange = line.UsedRange;
            int lineRows = lineRange.Rows.Count; //row number of line sheet

            line.Select(true);
            Excel.Range statRange = stat.UsedRange;
            int statRows = statRange.Rows.Count; //row number of stat sheet

            DateTime thisDay = DateTime.Today;
            int days = 0;
            int daysofescalation = 0;
            for (int i = 3; i <= mainRows; i++)
            {
                double percent = (i - 2) * 100 / (mainRows - 1); //progress percentage
                Console.Clear();
                Console.Write("Update in progress! Please wait..." + '\n' + percent + "%" );
                for(int j = 2; j <= cooisRows; j++)
                {
                    //get data from COOIS
                  if (coois.Cells[j,1].Value == tracking.Cells[i, 1].Value)
                    {
                        tracking.Cells[i, 2].Value = coois.Cells[j, 2].Value;
                        tracking.Cells[i, 5].Value = coois.Cells[j, 11].Value;
                        tracking.Cells[i, 7].Value = coois.Cells[j, 8].Value;
                        tracking.Cells[i, 8].Value = coois.Cells[j, 9].Value;

                        if ((thisDay - Convert.ToDateTime(tracking.Cells[i, 5].Value)).TotalDays > 0)
                        {
                            tracking.Cells[i, 38].Value = (thisDay - Convert.ToDateTime(tracking.Cells[i, 5].Value)).TotalDays; //calculate passed time in days format
                            days = Convert.ToInt32(tracking.Cells[i, 38].Value);
                        }
                        else
                        {
                            tracking.Cells[i, 38].Value = "Prod not yet started";
                        }


                        //A/B/C
                        if (Convert.ToString(tracking.Cells[i,2].Value).Length > 12)
                        {
                            tracking.Cells[i, 3].Value = "A";
                            tracking.Cells[i, 6].Value = Convert.ToDateTime(tracking.Cells[i, 5].Value).AddDays(21);
                            tracking.Range["J" + i + ":" + "AK" + i].Interior.ColorIndex = 0; //clear progress bar/line
                            tracking.Range["AF" + i + ":" + "AK" + i].Interior.ColorIndex = 1; //insert black part 

                            //fill up progress bar
                            if (Convert.ToString(tracking.Cells[i, 38].Value) != "Prod not yet started")
                            {
                                if (days > 21)
                                {
                                    tracking.Range["J" + i + ":" + "AE" + i].Interior.ColorIndex = 3;
                                }
                                else
                                {
                                    tracking.Range[tracking.Cells[i, 10], tracking.Cells[i, (10 + days)]].Interior.ColorIndex = 4;
                                }
                            }

                        }
                        if(Convert.ToString(tracking.Cells[i, 2].Value).Length == 12)
                        {
                            tracking.Cells[i, 3].Value = "B";
                            tracking.Cells[i, 6].Value = Convert.ToDateTime(tracking.Cells[i, 5].Value).AddDays(13);
                            tracking.Range["J" + i + ":" + "AK" + i].Interior.ColorIndex = 0; //clear progress bar/line
                            tracking.Range["W" + i + ":" + "AK" + i].Interior.ColorIndex = 1; //insert black part 

                            //fill up progress bar
                            if (Convert.ToString(tracking.Cells[i, 38].Value) != "Prod not yet started")
                            {


                                if (days >= 13)
                                {
                                    tracking.Range["J" + i + ":" + "V" + i].Interior.ColorIndex = 3;
                                }
                                else
                                {
                                    tracking.Range[tracking.Cells[i, 10], tracking.Cells[i, (10 + days)]].Interior.ColorIndex = 4;
                                }
                            }

                        }
                         if (Convert.ToString(tracking.Cells[i, 2].Value)[0] == '5')
                        {
                            tracking.Cells[i, 3].Value = "C";
                            tracking.Range["J" + i + ":" + "AK" + i].Interior.ColorIndex = 0; //clear progress bar/line
                            tracking.Cells[i, 6].Value = Convert.ToDateTime(tracking.Cells[i, 5].Value).AddDays(28);

                            //fill up progress bar
                            if (Convert.ToString(tracking.Cells[i, 38].Value) != "Prod not yet started")
                            {
                                if (days > 28)
                                {
                                    tracking.Range["J" + i + ":" + "AK" + i].Interior.ColorIndex = 3;
                                }
                                else
                                {
                                    tracking.Range[tracking.Cells[i, 10], tracking.Cells[i, (10 + days)]].Interior.ColorIndex = 4;
                                }
                            }

                        }
                         //get current production status
                        for (int l = 2; l <= statRows; l++)
                        {
                            if(tracking.Cells[i,1].Value == stat.Cells[l,1].Value && stat.Cells[l, 7].Value != null)
                            {
                                tracking.Cells[i, 9].value = Convert.ToString(stat.Cells[l, 4].Value);
                                l = statRows + 1;
                            }

                        }
                        //Get lines (way too slow, need to be optimised, maybe import it to a list?)
                        for (int k = 2; k <= lineRows; k++)
                        {
                            if (tracking.Cells[i, 2].Value == line.Cells[k, 1].Value)
                            {
                                tracking.Cells[i, 4].Value = line.Cells[k, 2].Value;
                                k = lineRows + 1;
                            }
                        }

                        if (tracking.Cells[i, 4].Value == null)
                        {
                            tracking.Cells[i, 4].Value = "NA";
                        }

                        //overdue status "YES" or "NO"
                        if(tracking.Cells[i, 10].Interior.ColorIndex == 3)
                        {
                            tracking.Cells[i, 39].Value = "Yes";
                        }
                        else
                        {
                            tracking.Cells[i, 39].Value = "No";
                        }

                        //escalation level one, making the body into a string, sending email and writing in the date of the escalation
                        if(tracking.Cells[i, 39].Value == "Yes" && tracking.Cells[i, 40].Value == null && tracking.Cells[i, 42].Value != "x")
                        {
                            body = "Dear Recipient," + "\n\nThe production order: " + Convert.ToString(tracking.Cells[i, 1].Value) + " for the material " + Convert.ToString(tracking.Cells[i, 2].Value) + " is overdue." + "\nOrdered quantity: " + Convert.ToString(tracking.Cells[i, 7].Value) + "\nConfirmed quantity: " + Convert.ToString(tracking.Cells[i, 8].Value) + "\nPlease take some actions." + "\n\n\n\nThis is an automatically generated message by the production tracking tool";

subject = Convert.ToString(tracking.Cells[i, 1].Value) + " production order is overdue - Escalalation I.";

                            Program.SendEMail(email1, subject, body);

                            tracking.Cells[i, 40].Value = thisDay;



                        }


                        //escalation level2
                        if (tracking.Cells[i, 40].Value != null)
                        {
                            daysofescalation = Convert.ToInt32((thisDay - Convert.ToDateTime(tracking.Cells[i, 40].Value)).TotalDays);

                            if(daysofescalation > 8 && tracking.Cells[i, 42].Value != "x" && tracking.Cells[i, 41].Value != null)
                            {
                                body = "Dear Recipient," + "\n\nThe production order: " + Convert.ToString(tracking.Cells[i, 1].Value) + " for the material " + Convert.ToString(tracking.Cells[i, 2].Value) + " is overdue." + "\nOrdered quantity: " + Convert.ToString(tracking.Cells[i, 7].Value) + "\nConfirmed quantity: " + Convert.ToString(tracking.Cells[i, 8].Value) + "\nPlease take some actions." + "\n\n\n\nThis is an automatically generated message by the production tracking tool";

subject = Convert.ToString(tracking.Cells[i, 1].Value) + " production order is overdue - Escalalation II.";

                                Program.SendEMail(email2, subject, body);

                                tracking.Cells[i, 41].Value = thisDay;

                            }

                        }

                        j = cooisRows + 1;
                    }
                 } 
            }

            wb.Close(true);
            excel.Quit();

            Console.Clear();
            Console.WriteLine("Update in progress!Please wait..." + '\n' + "100" + " % " + "\nExcel updated!");
            Console.ReadLine();

        }

        static void SendEMail(string email, string subject, string body)
        {
            Outlook.Application app = new Outlook.Application();
            Outlook.MailItem mailItem = app.CreateItem(Outlook.OlItemType.olMailItem);
            mailItem.Subject = subject;
            mailItem.To = email;
            mailItem.Body = body;
            mailItem.Importance = Outlook.OlImportance.olImportanceHigh;
            mailItem.Display(false);

            ((Outlook._MailItem)mailItem).Send();
        }

    }
}

非常感谢您的提前帮助,

-B

0 个答案:

没有答案