Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4036138
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T12:11:10+00:00 2026-05-20T12:11:10+00:00

This question is related to my previous question Link The method bellow does excatly

  • 0

This question is related to my previous question Link
The method bellow does excatly what I need, but I need to make it run faster.
Does anyone have any suggestion? Thanks.

        public bool createReport_NewMinusBase(string currentWorkingDirectory, string Book1, string Book2, double tolerance)
        {
            tolerance = 0.0001;
            myExcel.Application excelApp = new myExcel.Application();  // Creates a new Excel Application
            excelApp.Visible = false;  // Makes Excel visible to the user.
            excelApp.Application.DisplayAlerts = false;

            //useful for COM object interaction
            object missing = System.Reflection.Missing.Value;

            //Return value
            bool wereDifferences = false;

            //Comparison objects
            object objNew = null;
            object objBase = null;


            //source: http://www.codeproject.com/KB/office/csharp_excel.aspx
            //xlApp.Workbooks.Open(reportFolder + reportName, 0, false, 5, "", "", false, myExcel.XlPlatform.xlWindows, "", true, false, 0, true, false, false);

            //Open BASE FILE
            myExcel.Workbook excelWorkbook1 = excelApp.Workbooks.Open(@currentWorkingDirectory + Book1, 0,
                                              missing, missing, missing, missing, missing, missing, 
                                              missing,missing, missing, missing, missing, missing, missing);
            //OPEN NEW FILE
            myExcel.Workbook excelWorkbook2 = excelApp.Workbooks.Open(@currentWorkingDirectory + Book2, 0, 
                                              missing, missing, missing, missing, missing, missing, 
                                              missing, missing, missing, missing, missing, missing, missing);

            myExcel.Workbook excelWorkbook3 = excelApp.Application.Workbooks.Add(myExcel.XlWBATemplate.xlWBATWorksheet);

            myExcel.Worksheet wsBase;
            myExcel.Worksheet wsDiff;
            myExcel.Worksheet wsNew;


            try
            {
                wsBase = (myExcel.Worksheet)excelApp.Workbooks[Book1].Sheets["Sheet1"];
                wsNew = (myExcel.Worksheet)excelApp.Workbooks[Book2].Sheets["Sheet1"];
                wsDiff = (myExcel.Worksheet)excelWorkbook3.Worksheets.get_Item(1);
            }
            catch
            {
                throw new Exception("Excel file does not contain properly formatted worksheets");

            }


            //Copy Sheet from Excel Book "NEW" to "NEW(-)BASE"
            myExcel.Worksheet source_sheet;
            source_sheet = (myExcel.Worksheet)excelApp.Workbooks[Book2].Sheets["Sheet1"];
            source_sheet.UsedRange.Copy();
            wsDiff.Paste();




            //Determine working area
            int row = 0;
            int col = 0;
            int maxR = 0;
            int maxC = 0;

            int lr1 = 0;
            int lr2 = 0;
            int lc1 = 0;
            int lc2 = 0;
            {
                lr1 = wsNew.UsedRange.Rows.Count;
                lc1 = wsNew.UsedRange.Columns.Count;
            }

            {
                lr2 = wsBase.UsedRange.Rows.Count;
                lc2 = wsBase.UsedRange.Columns.Count;
            }

            maxR = lr1;
            maxC = lc1;

            if (maxR < lr2) maxR = lr2;
            if (maxC < lc2) maxC = lc2;

            //===================================================
            //Compare Cells
            //===================================================

            for (row = 1; row <= maxR; row++)
            {
                for (col = 1; col <= maxC; col++)
                {
                    //Get cell values
                    objNew = ((myExcel.Range)wsNew.Cells[row, col]).Value2;
                    objBase = ((myExcel.Range)wsBase.Cells[row, col]).Value2;


                    //If they are not equivilante
                    if (!equiv(objNew, objBase, tolerance))
                    {
                        wereDifferences = true;

                        //Mark differing cells
                        ((myExcel.Range)wsNew.Cells[row, col]).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red);
                        ((myExcel.Range)wsBase.Cells[row, col]).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red);

                        if ((objNew == null))
                        {                            
                            ((myExcel.Range)wsDiff.Cells[row, col]).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red);
                        }
                        else if (objNew.GetType().ToString() == "System.String")
                        {
                            ((myExcel.Range)wsDiff.Cells[row, col]).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red);
                        }
                        else
                        {
                            ((myExcel.Range)wsDiff.Cells[row, col]).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Yellow);
                            ((myExcel.Range)wsDiff.Cells[row, col]).Value2 = ((myExcel.Range)wsNew.Cells[row, col]).Value2 - ((myExcel.Range)wsBase.Cells[row, col]).Value2;
                        }
                    }
                    else //They are equivalent
                    {                        
                        if ((objNew == null))
                        {
                        }
                        else if (objNew.GetType().ToString() == "System.String")
                        {
                        }
                        else
                        {                            
                            ((myExcel.Range)wsDiff.Cells[row, col]).Value2 = ((myExcel.Range)wsNew.Cells[row, col]).Value2 - ((myExcel.Range)wsBase.Cells[row, col]).Value2;
                        }
                    }
                }
            }


            // Copy formatting
            myExcel.Range range1 = wsBase.get_Range((myExcel.Range)wsBase.Cells[1, 1], (myExcel.Range)wsBase.Cells[maxR, maxC]);
            myExcel.Range range2 = wsDiff.get_Range((myExcel.Range)wsDiff.Cells[1, 1], (myExcel.Range)wsDiff.Cells[maxR, maxC]);
            range1.Copy();
            range2.PasteSpecial(myExcel.XlPasteType.xlPasteColumnWidths);

            excelApp.Workbooks[Book1].Close(false, false, false);
            excelApp.Workbooks[Book2].Close(false, false, false);

            string Book3 = "reporttestpc.xlsx"; //"reportBaseMinusNew.xlsx"
            if (File.Exists(currentWorkingDirectory + Book3))
            {
                File.Delete(currentWorkingDirectory + Book3);
            }

            excelWorkbook3.SaveAs(currentWorkingDirectory + Book3, Type.Missing, Type.Missing,
                                 Type.Missing, false, false, myExcel.XlSaveAsAccessMode.xlNoChange,
                                 Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);

            //excelApp.Workbooks[Book3].Close(false, false, false);

            excelApp.Visible = true;
            return wereDifferences;
        }


        /// Determines whether two objects are equivalent
        /// Numbers are equivalent within the specified tolerance
        /// Strings are equivalent if they are identical
        /// obj1 and obj2 are the two objects being compared
        /// tolerance is the maximum difference between two numbers for them to be deemed equivalent

        private bool equiv(object obj1, object obj2, double tolerance)
        {
            if ((obj1 == null) && (obj2 == null))
            {
                return true;
            }
            else if ((obj1 == null) || (obj2 == null))
            {
                return false;
            }

            //if both are numeric
            if (IsNumeric(obj1))
            {
                if (IsNumeric(obj2))
                {
                    if (Math.Abs(Convert.ToDouble(obj2) - Convert.ToDouble(obj1)) < tolerance)
                    {
                        return true;    //If they are within tolerance
                    }
                    else
                    {
                        return false;   //If they are outside tolerance
                    }
                }
                else
                {
                    return false;       //If only one is numeric
                }
            }

            //Now assuming both are just random strings
            else
            {
                if ((string)obj1 == (string)obj2)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }


        // Test whether a given object represents a number
        internal static bool IsNumeric(object ObjectToTest)
        {
            if (ObjectToTest == null)
            {
                return false;
            }
            else
            {
                double OutValue;
                return double.TryParse(ObjectToTest.ToString().Trim(),
                    System.Globalization.NumberStyles.Any,
                    System.Globalization.CultureInfo.CurrentCulture,
                    out OutValue);
            }
        }

    ///
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T12:11:11+00:00Added an answer on May 20, 2026 at 12:11 pm

    Suggestion 1:

    Make:

    objNew.GetType().ToString() == "System.String"
    

    This:

    objNew is string
    

    Alot cleaner, right? Should be faster too.

    Suggestion 2:

    You make this call alot:

    ((myExcel.Range)wsDiff.Cells[row, col]
    

    Store the result in a variable and reuse it. All those indexers, properties, and boxing/unboxing will have overhead. Same goes for any place you are accessing the same COM method, property, or indexer over and over. Make sure you declare these variables outside of the scope of your for-loops also, you will hurt performance if they go in and out of scope for each iteration of the loop.

    Suggestion 3:

    Can you reuse your Excel Application object? Looks like you make a new one every time this method is called. Can you store it in a member variable in your class?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is kind of related to my previous question, but not really. I have
This question is related to (but perhaps not quite the same as): Does Django
This is somewhat related to my previous question but I've realised that I needed
This question is related to a previous post of mine Here . Basically, I
This question is directly related to my previous question ASP.NET AJAX Is it possible
Ok, so - this is heavily related to my previous question Transforming an object
[This question is related to but not the same as this one .] My
[This question is related to but not the same as this one .] If
This question is somewhat related to my previous question (not necessary to go through
This is related to my previous question here . I want 4 divs (absolute

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.