C
craigd
I'm writing to code that needs to check ALL text in a Workbook. The
initial solution - looping through the 'UsedRange' - works fine:
range = sheet.UsedRange;
for (int row = 1; row <= range.Rows.Count; row++) {
for (int col = 1; col <= range.Columns.Count; col++){
cell = (Excel.Range)range.Cells[row, col];
// do stuff with cell.Value2
}
}
BUT then when I tried it with a *complex* XLS (9 sheets, lots of
cells) it takes *forever* (almost 90 minutes) to process.
So I started investigating the 'SpecialCells' property which *should*
return a range with only 'matching' cells and not blanks.
Excel.Range newrange =
range.SpecialCells(Microsoft.Office.Interop.Excel.XlCellType.xlCellTypeConstants,
(object)3 );
To give you some idea - on Sheet 1 of my XLS, UsedRange.Count = 22,000
but SpecialCells returned newrange.Count = 192 (the data is quite
spread out). I'm thinking that will *significantly* reduce my
processing time...
HOWEVER, the range returned by SpecialCells has Rows=1 and Columns=1
(even though Count=192) which means I cannot iterate through it using
the loop shown above. I also can't iterate through it using FOREACH
because of http://support.microsoft.com/?kbid=328347
It looks like the range returned by SpecialCells is a combination of
the first matching cell, with the Cells collection containing the rest
- but I can't iterate through them!
The get_Item() method requires row & column parameters, but I can't
determine them either.
Any suggestions MUCH appreciated.
(e-mail address removed)
initial solution - looping through the 'UsedRange' - works fine:
range = sheet.UsedRange;
for (int row = 1; row <= range.Rows.Count; row++) {
for (int col = 1; col <= range.Columns.Count; col++){
cell = (Excel.Range)range.Cells[row, col];
// do stuff with cell.Value2
}
}
BUT then when I tried it with a *complex* XLS (9 sheets, lots of
cells) it takes *forever* (almost 90 minutes) to process.
So I started investigating the 'SpecialCells' property which *should*
return a range with only 'matching' cells and not blanks.
Excel.Range newrange =
range.SpecialCells(Microsoft.Office.Interop.Excel.XlCellType.xlCellTypeConstants,
(object)3 );
To give you some idea - on Sheet 1 of my XLS, UsedRange.Count = 22,000
but SpecialCells returned newrange.Count = 192 (the data is quite
spread out). I'm thinking that will *significantly* reduce my
processing time...
HOWEVER, the range returned by SpecialCells has Rows=1 and Columns=1
(even though Count=192) which means I cannot iterate through it using
the loop shown above. I also can't iterate through it using FOREACH
because of http://support.microsoft.com/?kbid=328347
It looks like the range returned by SpecialCells is a combination of
the first matching cell, with the Cells collection containing the rest
- but I can't iterate through them!
The get_Item() method requires row & column parameters, but I can't
determine them either.
Any suggestions MUCH appreciated.
(e-mail address removed)