Home > Backend Development > PHP Tutorial > php writes data to excel

php writes data to excel

WBOY
Release: 2016-08-08 09:22:28
Original
1887 people have browsed it

//header("Content-type: text/html; charset=utf-8");
//include_once 'PHPExcel.php';

/**

     * 生成excel
     * @param $list
     */
    public function excel($list,$excelName='brand') {
        // 创建新的PHPExcel对象    
        $objPHPExcel = new PHPExcel();  
        // 设置属性    
        $objPHPExcel->getProperties()->setCreator("ctos")  
                ->setLastModifiedBy("ctos")  
                ->setTitle("Office 2007 XLSX Test Document")  
                ->setSubject("Office 2007 XLSX Test Document")  
                ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")  
                ->setKeywords("office 2007 openxml php")  
                ->setCategory("Test result file");  
      
        // 字体宽度    
        $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(20);  
        $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(40);  
      
        // 设置行高度    
        $objPHPExcel->getActiveSheet()->getRowDimension('1')->setRowHeight(22);
        $objPHPExcel->getActiveSheet()->getRowDimension('2')->setRowHeight(20);   
      
        // 设置水平居中    
        $objPHPExcel->getActiveSheet()->getStyle('A')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);  
        $objPHPExcel->getActiveSheet()->getStyle('B')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);  
        // 表头  
        $objPHPExcel->setActiveSheetIndex(0)  
                ->setCellValue('A1', '品牌')  
                ->setCellValue('B1', '系列');  
        // 内容  
        $i = 2;
        foreach($list as $key => $item ){
            foreach($item as $k => $v ){    
                $objPHPExcel->getActiveSheet(0)->setCellValue('A'.$i, $key);  
                $objPHPExcel->getActiveSheet(0)->setCellValue('B'.$i, $v['goname'].'__'.$v['name']);  
                ++$i;
            }
        }
                                                                                                                
             $objPHPExcel->setActiveSheetIndex(0);                                                                                                                                                        $ excelName . '.xls"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save( 'php://output');
}
The above introduces how to write data to excel in PHP, including the relevant aspects. I hope it will be helpful to friends who are interested in PHP tutorials.


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template