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.
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();
object[,] arrayValues = cells.Value;
object[,] arrayText = (object[,])Array.CreateInstance(typeof(object),
new int[] {
arrayValues.GetUpperBound(0),
arrayValues.GetUpperBound(1)
},
new int[] {
arrayValues.GetLowerBound(0),
arrayValues.GetLowerBound(1)
});
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.