7

Is there anyway to set the same properties (colors, row height, alignments) and content (hear names) for all sheets at once with PHPExcel? How?

Thank you.

Francisc
  • 77,430
  • 63
  • 180
  • 276

1 Answers1

13

If you're creating all those sheets yourself. Set the properties for the first sheet that you create, then clone that sheet and attach the new clone back to the same workbook. This should copy all existing cell data and style information from the original worksheet.

//  Create a new PHPExcel object with a single sheet
$objPHPExcel = new PHPExcel();

//  Set any styles here against the currently active sheet in $objPHPExcel

//  Get the current sheet with all its newly-set style properties
$objWorkSheetBase = $objPHPExcel->getSheet();

//  Create a clone of the current sheet, with all its style properties
$objWorkSheet1 = clone $objWorkSheetBase;
//  Set the newly-cloned sheet title
$objWorkSheet1->setTitle('Cloned Sheet');
//  Attach the newly-cloned sheet to the $objPHPExcel workbook
$objPHPExcel->addSheet($objWorkSheet1);

It's useful to note that you can set styles for a cell even before you write data to that cell

Mark Baker
  • 209,507
  • 32
  • 346
  • 385
  • Hi, Mark. I meant more like: `$objPHPExcel->getAllSheets()->getColumnDimension('A')->setWidth(20);`. Is something like that possible? I am currently looping through an array of sheets. – Francisc Dec 22 '10 at 13:50
  • 1
    @Francisc - No, there's nothing like that for setting properties across all sheets in a workbook with a single call. It's not something that anybody has ever asked for before. Looping through the each sheet in turn is currently the only option available. – Mark Baker Dec 22 '10 at 13:55
  • Well I have a large database that I need to structure to have no more than 50K rows on a single sheet but all the rest of the information, like alignments, sizes, colours etc are to be identical. – Francisc Dec 22 '10 at 13:58
  • 1
    If that's the case, then set the styles against your initial worksheet and create a clone of that as a template before you write any data. Then, when you need to create a new worksheet for the next 50k rows, clone the template clone and attach the new clone (using the code snippet in my example above) rather than calling createSheet() to create the next worksheet – Mark Baker Dec 22 '10 at 14:05