I have some C# code which programatically loops over all the cells in the spread sheet and convert any numeric string values to actual numbers.
It seems to work, but it runs fairly slow(on a spread sheet with 40 columns by 3000 rows, it takes over a minute to run):
private void SetNumberStringsToValues(Excel.Range range)
{
object[,] values = range.get_Value(Excel.XlRangeValueDataType.xlRangeValueDefault);
Parallel.For(1, range.Columns.Count, i =>
{
for (var j = 1; j < range.Rows.Count; j++)
{
var doubleValue = 0.0;
if (double.TryParse(values[j, i].ToString(), out doubleValue))
values[j, i] = doubleValue;
}
});
range.set_Value(Excel.XlRangeValueDataType.xlRangeValueDefault, values);
}
Does anyone know a more efficient way of doing this?
Try caching the number of rows instead of calling range.Rows.Count inside the loop. There may be some internal synchronization in the
rangeinstance that will come under contention when accessed from multiple threads.Also, I’m curious why you start your enumeration at 1 instead of 0? Won’t that miss the first row and column?