Formatting a lot of Cells causes EInvalidOperator Exception

Questions and answers on how to use XLSReadWriteII 3/4/5.
Post Reply
vabene
Posts: 2
Joined: Wed Dec 07, 2011 12:04 pm

Formatting a lot of Cells causes EInvalidOperator Exception

Post by vabene »

If i try to format or reformat a lot of several cells i get an EInvalidOperator-Exception.

When i look into the Taskmanager, i see that the Process is createing to much GDI-Objects, when it reaches 10.000 the exception is thrown in my case.

My excel file have around 3.000 lines atm. I tried to format specific cells with another color:

Sheets[i].Cell[c,r].FillPatternForeColor := xcGray25;

I could do a workaround, to format the column instead of each cell, and reformat the cells i don't want to have a specific color. It's ok for the moment but not acceptable for me, cause i need to do more formats. And i always must be afraid of crashing my tool every time the amount of my data is growing;

btw: are my cells losing the standard Excel-Grid, when formating with a color, is there a way to get this back?

I simplified the code as much as i could, to show the main aspekts i'm using XLSReadWrite and formatting the excel file, before i did the workaraound.
Maybe i'm doing a mistake?

with TXLSReadWriteII4.Create(nil) do
try
Version := xvExcel2007;
BeginUpdate;
for r := 0 to lIndexProductList.Count - 1 do
begin
Sheets[0].AsWideString[0,r] := lIndexProduct.Name;
Sheets[0].AsWideString[1,r] := FormatDateTime(lIndexProduct.EditMask, lIndexProduct.SOLDate);
Sheets[0].AsWideString[2,r] := lIndexProduct.Sku;
Sheets[0].AsWideString[3,r] := lIndexProduct.EanCode;
aSheet.Cell[0,r].FillPatternForeColor := xcGray25; //-- several cell formatting
aSheet.Cell[0,r].BorderTopStyle := cbsThin;
aSheet.Cell[0,r].BorderBottomStyle := cbsThin;

aSheet.Cell[1,r].FillPatternForeColor := xcGray25;
aSheet.Cell[1,r].BorderTopStyle := cbsThin;
aSheet.Cell[1,r].BorderBottomStyle := cbsThin;

aSheet.Cell[2,r].FillPatternForeColor := xcGray25;
aSheet.Cell[2,r].BorderTopStyle := cbsThin;
aSheet.Cell[2,r].BorderBottomStyle := cbsThin;

aSheet.Cell[3,r].FillPatternForeColor := xcGray25;
aSheet.Cell[3,r].BorderTopStyle := cbsThin;
aSheet.Cell[3,r].BorderBottomStyle := cbsThin;
end;
Sheets[0].AutoWidthCol(3);
EndUpdate;
asRCName := FsRcName;
if aStream <> nil then
WriteToStream(aStream)
else
Write;
finally
Free;
end;
larsa
Site Admin
Posts: 926
Joined: Mon Jun 27, 2005 9:30 pm

Re: Formatting a lot of Cells causes EInvalidOperator Exception

Post by larsa »

Hello

I have to check this more, but it's better to format a whole row or column, than formatting individual cells. When a cell is formatted, the formatting data is stored for each cell. If you instead format a row or a column, the formatting data is only stored once.
Lars Arvidsson, Axolot Data
Post Reply