Copy Existing Styles to New Cells

Topics: User Forum
Nov 20, 2009 at 12:09 AM

I have a pre-styled Excel workbook. I would like to add more rows to the Excel file and copy the existing styles to the new rows that I add. How can I copy a style config from one cell to another cell?

Developer
Nov 20, 2009 at 4:18 AM

Copy-paste is something that is on my to-do list, but it is not the highest thing on the list right now.

What you may try to do to copy and paste a style from cell B2 to H10 is this:

$xfIndex = $worksheet->getCell('B2')->getXfIndex(); // returns integer e.g. 5
$worksheet->getCell('H10')->setXfIndex($xfIndex);

Such methods have not been mentioned in the documentation and the above may or may not give the desired result. There may be complications if there are e.g. row styles and column styles.

Mar 22, 2010 at 5:09 PM

I want to acomplish the same thing.

I also would like to save the effort of iterating over the cells and apply the style.

Anyone knows a better way to do it?

Mar 31, 2010 at 3:16 PM

You could always use duplicateStyle (from a PHPExcel_Worksheet object).

For example:

$worksheet->duplicateStyle($worksheet->getStyle('A1'), 'A2:L87');
 Note that duplicateStyle also accepts single cell reference. And note that it's much more resource intensive (in my experience) than using other methods (applyFromArray, for one).

Jul 11, 2011 at 9:49 AM

Thank you Technoh for bringing to the surface the duplicate style method

It is a nice way to copy the style from one cell to another (or a range of cells as you specified) in PHPExcel

I am just explaining it in order to help google help others who search with keywords like "copy style phpexcel" ;)

Oct 9, 2012 at 2:37 PM

Thanks to all of you and Google for making this easy to find :)