Codeigniter& PHPexcel字体大小在第一行没有变化

时间:2017-09-30 07:27:43

标签: codeigniter phpexcel

当我创建一个phpexcel文档时,我设置了一个自定义字体大小。

由于某种原因,第一行的字体大小没有变化。

  

问题:如何确保所有行具有相同的大小,包括标题行

我在主控制器的foreach循环中有代码但是错过了第一行单元格。

enter image description here

    $styleArray = array(
        'font'  => array(
        'size'  => 15,
        'name'  => 'Candara'
        ));

        $objPHPExcel->getActiveSheet()->getStyle('A' . $excelrow . ':D' . $excelrow)->applyFromArray($styleArray);


$objPHPExcel->getActiveSheet()->getStyle($objPHPExcel->getActiveSheet()->calculateWorksheetDimension())->applyFromArray($styleArray2);

控制器

<?php

class Events extends MX_Controller {


    public function generate_excel() {

        $query = $this->db->get('event');
        $excelresults = $query->result_array();

        require (APPPATH . 'third_party/PHPExcel-1.8/Classes/PHPExcel.php');
        require (APPPATH . 'third_party/PHPExcel-1.8/Classes/PHPExcel/Writer/Excel2007.php');

        $objPHPExcel = new PHPExcel();

        $objPHPExcel->getProperties()->setCreator("");
        $objPHPExcel->getProperties()->setLastModifiedBy("");
        $objPHPExcel->getProperties()->setSubject("");
        $objPHPExcel->getProperties()->setCreator("");
        $objPHPExcel->getProperties()->setDescription("");

        $objPHPExcel->setActiveSheetIndex(0);

        $objPHPExcel->getActiveSheet()->SetCellValue("A1", 'Event');
        $objPHPExcel->getActiveSheet()->SetCellValue("B1", 'Event Title');
        $objPHPExcel->getActiveSheet()->SetCellValue("C1", 'Event Date');
        $objPHPExcel->getActiveSheet()->SetCellValue("D1", 'Event Start Time');

        $objPHPExcel->getActiveSheet()->getPageSetup()->setOrientation(PHPExcel_Worksheet_PageSetup::ORIENTATION_LANDSCAPE);
        $objPHPExcel->getActiveSheet()->getPageSetup()->setPaperSize(PHPExcel_Worksheet_PageSetup::PAPERSIZE_A4);

        $objPHPExcel->getActiveSheet()->setPrintGridlines(TRUE);

        $objPHPExcel->getActiveSheet()->getRowDimension(1)->setRowHeight(-1);

        $excelrow = 2;

        foreach ($excelresults as $excelresult => $excelvalue) {

            $styleArray = array(
            'font'  => array(
            'size'  => 15,
            'name'  => 'Candara'
            ));

            $objPHPExcel->getActiveSheet()->getStyle('A' . $excelrow . ':D' . $excelrow)->applyFromArray($styleArray);

            if (!empty($excelvalue['fill_color'])) {
                     // Set the fill style
            $objPHPExcel->getActiveSheet()->getStyle('A' . $excelrow . ':D' . $excelrow)->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
                    // Set the colour for the fill
            $objPHPExcel->getActiveSheet()->getStyle('A' . $excelrow . ':D' . $excelrow)->getFill()->getStartColor()->setRGB($excelvalue['fill_color']);

                $styleArray1 = array(
                    'font'  => array(
                        'color' => array('rgb' => $excelvalue['font_color']),
                    )
                );

            $objPHPExcel->getActiveSheet()->getStyle('A' . $excelrow . ':D' . $excelrow)->applyFromArray($styleArray1);

            $objPHPExcel->getActiveSheet()->getStyle('A' . $excelrow . ':D' . $excelrow)->getAlignment()->setIndent(2);


            }

            $objPHPExcel->getActiveSheet()->SetCellValue("A" . $excelrow, $excelvalue['event']);
            $objPHPExcel->getActiveSheet()->SetCellValue("B" . $excelrow, $excelvalue['event_title']);
            $objPHPExcel->getActiveSheet()->SetCellValue("C" . $excelrow, $excelvalue['event_date']);
            $objPHPExcel->getActiveSheet()->SetCellValue("D" . $excelrow, $excelvalue['event_start_time']);

            $excelrow++;


        }

        ///exit();

        $filename = 'Bowling-Events-For-' . date('Y') . '.xlsx';

        $objPHPExcel->getProperties()->setTitle("Riwaka Bowling Club Events");

        header("Content-Type: application/vnd.ms-excel; charset=utf-8"); # Important 
        header("Content-Type: application/force-download");
        header("Content-Type: application/octet-stream");
        header("Content-Type: application/download");

        header("Content-Disposition: attachment; filename=".$filename."");
        header("Content-Transfer-Encoding: binary");

        header("Pragma: no-cache");

        header("Expires: 0");

        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
        header("Cache-Control: private",false);

        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
        $objWriter->save('php://output');

        exit();
    }
}

1 个答案:

答案 0 :(得分:0)

您确定已附上正确的密码吗?或者您确定已经测试了附加的代码吗?

我已经测试了您的代码,并且我的工作正常。

my screenshot