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

  • Home
  • SEARCH
  • 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 65495
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T18:57:19+00:00 2026-05-10T18:57:19+00:00

What is the most efficient way to enumerate every cell in every sheet in

  • 0

What is the most efficient way to enumerate every cell in every sheet in a workbook?

The method below seems to work reasonably for a workbook with ~130,000 cells. On my machine it took ~26 seconds to open the file and ~5 seconds to enumerate the cells . However I’m no Excel expert and wanted to validate this code snippet with the wider community.

DateTime timer = DateTime.Now; Microsoft.Office.Interop.Excel.Application excelApplication = new Microsoft.Office.Interop.Excel.Application(); try {     exampleFile = new FileInfo(Path.Combine(System.Environment.CurrentDirectory, 'Large.xlsx'));     excelApplication.Workbooks.Open(exampleFile.FullName, false, false, missing, missing, missing, true, missing, missing, true, missing, missing, missing, missing, missing);     Console.WriteLine(string.Format('Took {0} seconds to open file', (DateTime.Now - timer).Seconds.ToString()));      timer = DateTime.Now;     foreach(Workbook workbook in excelApplication.Workbooks)     {             foreach(Worksheet sheet in workbook.Sheets)             {             int i = 0, iRowMax, iColMax;             string data = String.Empty;              Object[,] rangeData = (System.Object[,]) sheet.UsedRange.Cells.get_Value(missing);              if (rangeData != null)             {                 iRowMax = rangeData.GetUpperBound(0);                                        iColMax = rangeData.GetUpperBound(1);                                                                         for (int iRow = 1; iRow < iRowMax; iRow++)                 {                         for(int iCol = 1; iCol < iColMax; iCol++)                     {                         data = rangeData[iRow, iCol] != null ? rangeData[iRow, iCol].ToString() : string.Empty;                         if (i % 100 == 0)                         {                             Console.WriteLine(String.Format('Processed {0} cells.', i));                         }                          i++;                     }                                                                                                                    }                }         }          workbook.Close(false, missing, missing);     }      Console.WriteLine(string.Format('Took {0} seconds to parse file', (DateTime.Now - timer).Seconds.ToString()));                   }     finally     {         excelApplication.Workbooks.Close();                      excelApplication.Quit();                       }                    

Edit:

Worth stating that I want to use PIA and interop in order to access properties of excel workbooks that are not exposed by API’s that work directly with the Excel file.

  • 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. 2026-05-10T18:57:20+00:00Added an answer on May 10, 2026 at 6:57 pm

    Excel PIA Interop is really slow when you are doing things cell by cell.

    You should select the range you want to extract, like you did with the Worksheet.UsedRange property and then read the value of the whole range in one step, by invoking get_Value() (or just simply by reading the Value or Value2 property, I can’t remember which one) on it.

    This will yield an object[,], that is, a two dimensional array, which can be easily enumerated and is quick to be read.

    EDIT: I just read your actual code and realized that it actually does what I proposed. Shame on me for not reading the question properly before answering. In that case, you cannot make it much faster. Excel PIA Interop is slow. If you need a quicker solution you will have to either migrate jExcelApi from Java to C# (not a terribly hard thing to do) or use some commercial component. I suggest to avoid the OLEDB interface at all costs, in order to keep your sanity.

    Unrelated, but helpful tip: You should use the ?? operator. It is really handy. Instead of

    data = rangeData[iRow, iCol] != null ? rangeData[iRow, iCol].ToString() : string.Empty; 

    you could just write

    data = Convert.ToString(rangeData[iRow, iCol]) ?? string.Empty; 

    In that case, even String.Empty is not necessary since Convert.ToString(object) converts null to an empty string anyway.

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

Sidebar

Related Questions

What is the most efficient way to go through and update every single node
What's the most efficient way to concatenate strings?
What's the most efficient way to resize large images in PHP? I'm currently using
What is the most efficient way to determine how many comments a particular blog
What's the most efficient way to convert the output of this function from a
Whats the most efficient way of selecting total number of records from a large
What would be the most efficient way of recording to a log (.txt) from
What is the most efficient way to get the default constructor (i.e. instance constructor
What is the most efficient way of turning the list of values of a
What is the most efficient way of ignoring case, punctuation, and whitespace in strings?

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.