菜单

PHPExcel demo function

2012年09月6日 - php

直接使用, 数据第一个元素是column name.

/**
     * generate Excel by PHPExcel
     */
    function generateListExcel($excelarray, $outputName, $title='', $type='2007', $description=''){
        set_time_limit(0);
        //set php config
        if(intval(get_cfg_var('memory_limit'))<1024)ini_set('memory_limit', '1024M');
        //$outputName = iconv('utf-8','iso-8859-1',$outputName);
        // Create new PHPExcel object
        $objPHPExcel = new PHPExcel();
        // Set properties
        $objPHPExcel->getProperties()->setCreator($GLOBALS['TSFE']->fe_user->user['first_name'].' '.$GLOBALS['TSFE']->fe_user->user['last_name'])
            ->setLastModifiedBy($GLOBALS['TSFE']->fe_user->user['first_name'].' '.$GLOBALS['TSFE']->fe_user->user['last_name'])
            ->setTitle($title)
            ->setSubject($title)
            ->setDescription($description)
            ->setKeywords($this->pi_getLL('excel_keywords'))
            ->setCategory($title);

        // Set default font
        $objPHPExcel->getDefaultStyle()->getFont()->setName('Calibri');
        $objPHPExcel->getDefaultStyle()->getFont()->setSize(12);
        $objPHPExcel->getDefaultStyle()->getFont()->setBold(true);
        // Add some data
        $styleArray = array(
            'alignment' => array(
                'rotation' => 90,
                'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
            ),
        );
        $styleArrayBorder = array(
            'borders' => array(
                'outline' => array(
                    'style' => PHPExcel_Style_Border::BORDER_THIN,
                    'color' => array('argb' => '00000000'),
                ),
            ),
        );
        $styleArrayCenter = array(
            'alignment' => array(
                'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
            ),
        );
        $excelX = range("A","Z");
        foreach(range("A","C") as $val1){
            foreach(range("A","Z") as $val2){
                $excelX[] = $val1.$val2;
            }
        }
        for ($i=0;$igetActiveSheet()->getColumnDimension($excelX[$j])->setAutoSize(true);	//auto size
                if($i==0){
                    //$objPHPExcel->getActiveSheet()->getStyle($excelX[$j].($i+1))->applyFromArray($styleArray);
                    //$objPHPExcel->getActiveSheet()->getColumnDimension($excelX[$j])->setWidth(20);
                    //$objPHPExcel->getActiveSheet()->getStyle($excelX[$j].($i+1))->getFont()->setItalic(true);
                    $objPHPExcel->getActiveSheet()->getStyle($excelX[$j].($i+1))->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
                    $color = 'dff2f7';
                    $objPHPExcel->getActiveSheet()->getStyle($excelX[$j].($i+1))->getFill()->getStartColor()->setRGB($color);
                    $objPHPExcel->getActiveSheet()->getRowDimension($i+1)->setRowHeight(40);
                }else{
                    $objPHPExcel->getActiveSheet()->getRowDimension($i+1)->setRowHeight(30);
                    //if($j>=count($excelarray[0])-3){
                    //    $objPHPExcel->getActiveSheet()->getStyle($excelX[$j].($i+1))->getFont()->setBold(false);
                    //}
                }
                $objPHPExcel->getActiveSheet()->getStyle($excelX[$j].($i+1))->applyFromArray($styleArrayBorder);
                $objPHPExcel->setActiveSheetIndex(0)->setCellValue($excelX[$j].($i+1), $tempval);
            }
        }
        //insert new column to excel
        //$objPHPExcel->getActiveSheet()->insertNewColumnBefore('E', 1);

        // Rename sheet
        $objPHPExcel->getActiveSheet()->setTitle($title);
        //auto filter
        //$objPHPExcel->getActiveSheet()->setAutoFilter('A1:'.end($excelX).count($excelarray));
        //$objPHPExcel->getActiveSheet()->getStyle('A1:'.end($excelX).'1')->applyFromArray($styleArrayCenter);
        $objPHPExcel->getActiveSheet()->getStyle('A1:'.end($excelX).count($excelarray))->getAlignment()->setVertical(PHPExcel_Style_Alignment::HORIZONTAL_CENTER)->setWrapText(true);
        // Set active sheet index to the first sheet, so Excel opens this as the first sheet
        //$objPHPExcel->setActiveSheetIndex(0);
        if($type=='2003'){
            // Redirect output to a client’s web browser (Excel5)
            header('Content-Type: application/vnd.ms-excel');
            header('Content-Disposition: attachment;filename="'.$outputName.'.xls"');
            header('Cache-Control: max-age=0');
            $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
            $objWriter->save('php://output');
        }else{
            // Redirect output to a client’s web browser (Excel2007)
            header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
            header('Content-Disposition: attachment;filename="'.$outputName.'.xlsx"');
            header('Cache-Control: max-age=0');
            $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
            $objWriter->save('php://output');
        }
        exit;
    }

发表评论

电子邮件地址不会被公开。 必填项已用*标注