dongmi1941 2015-03-27 09:45
浏览 107
已采纳

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

I am using phpexcel library. but when I want to set the font color, and different style for a cell value then when I export my file then those changes not applied there. my code is below

<?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'

?>
  • 写回答

1条回答 默认 最新

  • dti3914 2015-03-27 10:15
    关注

    At the bottom after ob_start(); replace all with the following code

    // 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;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么