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 WPF动态创建页面内容
  • ¥15 如何对TBSS的结果进行统计学的分析已完成置换检验,如何在最终的TBSS输出结果提取除具体值及如何做进一步相关性分析
  • ¥15 SQL数据库操作问题
  • ¥100 关于lm339比较电路出现的问题
  • ¥15 Matlab安装yalmip和cplex功能安装失败
  • ¥15 加装宝马安卓中控改变开机画面
  • ¥15 STK安装问题问问大家,这种情况应该怎么办
  • ¥15 关于罗技鼠标宏lua文件的问题
  • ¥15 halcon ocr mlp 识别问题
  • ¥15 已知曲线满足正余弦函数,根据其峰值,还原出整条曲线