Problems copying a sheet


I have to add a new sheet having the column widths and the row heigths equals to those of a pre-existent sheet, e.g. the first one.

To do this I use this code:


as expected the new sheet has the same content of Sheet1, but (unlike what happens when I copy a worksheet in Excel) it does not have the same column widths or row heights, it has the default size.

At this point I have tried to set the width of the first 5 columns of the new sheet in this way

Xls.ActiveSheet := 1;

FColDefaultWidth := Xls.GetColWidth(1);


Xls.ActiveSheet := Xls.SheetCount;


but again I do not get the desired result.

Could you help me figure out where I'm wrong?

Thank you

Massimo Marchegiani

I am not sure on what is happening here, but column widths (and row heights) are of course copied when you copy sheets, same as in Excel, and have been like this since day 1. On its first public release, about 2001, FlexCel was a report generator and it supported multi sheet reports. So the API did basically nothing, but InsertAndCopyRows and InsertAndCopySheets were there and working.

I made a simple example here:

And as you can see, the column widths are copied to the second sheet. Is there anything you are doing differently, or do you think that you can change the example to show the case you see?

Ok Adrian,

I understood where it's wrong.

Obviuosly the function Xls.CopyAndInsertSheet works fine.

The fact is that I immediatly after this function I call the procedure Xls.ClearSheet that (with same surprise) do not behaves like excel because delete all custom widths and heights...

Thank You

Ok, thanks for letting me know. ClearSheet indeed removes everything from the sheet, if you want to just clear the data use DeleteRange instead with TXlsCellRange.FullRange as parameter for the range to delete. With some overloads of DeleteRange you can also decide if you want to clear just the data, or the data and the formats.


Thank You