简体   繁体   中英

PHPExcel: Can't style merged cells

I can't figure out how to set font styles for merged cells. Either by opening an XLS file with merged cells, or creating a doc from scratch with PHPExcel, merged cells won't style, they insist in being too TimesNewRomany for my needs.

Here's an abbreviated version of my code:

$excel = new PHPExcel();
$excel->setActiveSheetIndex(0);

# I know beforehand my spreadsheet spans from A1 to G42,
# so I set styles for all those cells:
$excel
  ->getActiveSheet()
  ->getStyle('A1:G42')
  ->getFont()
  ->setName('Helvetica')
  ->setSize(8);

# Now, onto some merging:
$sheet = $excel->getActiveSheet();
$sheet->mergeCells('D2:G2');

Then I go on and ouput it all as PDF using DomPDF. All my cells look nice and tidy wearing Helvetica@8px, except the merged ones. Not even targeting them specifically:

$sheet->getStyle('D2:G2')->getFont()->setName('Helvetica')->setSize(8);

What I've tried so far, from Google results:

  • Using applyFromArray() on cells
  • Setting styles after merging cells
  • Using getActiveSheet()->getDefaultStyle()->applyFromArray()

Any additional ideas?

I'm not sure if this is the only way to solve this issue, but apparently I was using an outdated versión of PHPExcel, wich presumably had trouble handling merged cells.

I've just updated the library to the latest source from Github and problems went away. So, I have to assume it just wasn't supported in the old versions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM