Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The Excel spreadsheet should be read by .NET. It is very efficient to read all values from the active range by using the property Value. This transfers all values in a two dimensional array, by one single call to Excel.

However reading strings is not possible for a range which contains more than one single cell. Therefor we have to iterate over all cells and use the Text property. This shows very poor performance for larger document.

The reason of using strings rather than values is to obtains the correct format (for instance for dates or the number of digits).

Here is a sample code written in C# to demonstrate the approach.

C#
static void Main(string[] args)
{
    Excel.Application xlApp = 
        (Excel.Application)System.Runtime.InteropServices.Marshal.
            GetActiveObject("Excel.Application");

	var worksheet = xlApp.ActiveSheet;
	var cells = worksheet.UsedRange();

	// read all values in array -> fast
	object[,] arrayValues = cells.Value;

	// create array for text of the same extension
	object[,] arrayText = (object[,])Array.CreateInstance(typeof(object),
            new int[] {
                arrayValues.GetUpperBound(0),
                arrayValues.GetUpperBound(1)
            },
            new int[] {
                arrayValues.GetLowerBound(0),
                arrayValues.GetLowerBound(1)
           });

	// read text for each cell -> slow
	for (int row = arrayValues.GetUpperBound(0);
             row <= arrayValues.GetUpperBound(0);
             ++row)
        {
            for (int col = arrayValues.GetUpperBound(0);
                 col <= arrayValues.GetUpperBound(1);
                 ++col)
            {
                object obj = cells[row, col].Text;
                arrayText[row, col] = obj;
            }
	}
}


The question is, if there is a more efficient way to read the complete string content from an Excel document.

What I have tried:

One idea was to use cells.Copy to copy the content to the clipboard to get it from there. However this has some restrictions and could of course interfere with users which are working with the clipboard at the same time.
Posted
Updated 30-Oct-19 2:27am
Comments
EricERankin 15-Apr-20 0:41am    
Excel Interop is not known for its efficiency, it is a wrapper for GUI application so the performances can be questionable at times, especially in case of large files.
If you can change your approach, then here is an alternative that uses GemBox.Spreadsheet to retrieve faster all the cells formatted values:

----
var workbook = ExcelFile.Load("input.xlsx");
var worksheet = workbook.Worksheets.ActiveWorksheet;

int rowCount = worksheet.Rows.Count;
int columnCount = worksheet.CalculateMaxUsedColumns();

string[,] values = new string[rowCount, columnCount];
for (int r = 0; r < rowCount; r++)
for (int j = 0; j < columnCount; j++)
values[r, j] = worksheet.Cells[r, j].GetFormattedValue();
----

Also, here is another example for reading Excel files from C# in a clean and efficient way.

1 solution

Maybe you can use get_Range(), see: How to read an Excel spreadsheet in c# quickly - Stack Overflow[^]

I'm using the range by the following line: var cells = worksheet.UsedRange();

The example at Stack Overflow is using .Value like I've done in my example to show that it works fast. After that is converts the object to string. This does of course not include the formats in Excel, so it does not fit the requirement.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900