Phpexcel - 如何设置单元格值的字体格式

问题描述 投票:0回答:1

我正在使用phpexcel库。但是当我想设置字体颜色和单元格值的不同样式时,那么当我导出我的文件时,那些更改不会在那里应用。我的代码如下

<?php 
    require_once (SPEE_PLUGIN_DIRS . "/Classes/PHPExcel.php");

    require_once (SPEE_PLUGIN_DIRS . "/Classes/PHPExcel/IOFactory.php");

    $objPHPExcel = new PHPExcel();

    $styleArray = array(
                       'font'  => array(
                       'bold'  => true,
                       'color' => array('rgb' => 'FF0000'),
                       'size'  => 15,
                       'name'  => 'Verdana'
                       ));                                       

    $objPHPExcel->setActiveSheetIndex(0);

    $objPHPExcel->setActiveSheetIndex(0)->mergeCells('A1:H1');

    $objPHPExcel->setActiveSheetIndex(0)->mergeCells('A2:H2');

    $objPHPExcel->setActiveSheetIndex(0)->mergeCells('A3:H3');

    $objPHPExcel->setActiveSheetIndex()->getStyle('E1')->applyFromArray(array("font" => array( "bold" => true))); // font not set bold.

    $objPHPExcel->getActiveSheet()->setCellValue('E1', 'my data');

    $objPHPExcel->getActiveSheet()->getCell('A7')->setValue('CNIC No');  

    $objPHPExcel->getActiveSheet()->getStyle('A7')->applyFromArray($styleArray); // this style array not work on 'A7'

?>
php wordpress phpexcel
1个回答
0
投票

在ob_start()之后的底部;用以下代码替换all

// Redirect output to a client’s web browser (Excel5)
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="01simple.xls"');
header('Cache-Control: max-age=0');
// If you're serving to IE 9, then the following may be needed
header('Cache-Control: max-age=1');

// If you're serving to IE over SSL, then the following may be needed
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always  modified
header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header ('Pragma: public'); // HTTP/1.0

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
exit;
© www.soinside.com 2019 - 2024. All rights reserved.