TP5 实现数据生成excel并下载

1,下载

实现TP5 实现数据生成excel并下载,首先下载依赖PHPExcel ,地址: https://github.com/PHPOffice/PHPExcel。

2,放置

将下载好的压缩包解压,取出Classes文件夹放置在框架中的Vendor目录下,并重命名为PHPExcel。

3,PHP代码示例

 

/**
 * 导出excel文件
 * @return mixed
 */
public function down() {
    $list = model('user')->order('id asc')->select()->toArray();

    vendor("PHPExcel.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(8);
    $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(20);
    $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(10);
    $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(10);
    $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(50);

    //设置行高度
    $objPHPExcel->getActiveSheet()->getRowDimension('1')->setRowHeight(22);

    $objPHPExcel->getActiveSheet()->getRowDimension('2')->setRowHeight(20);

    //set font size bold
    $objPHPExcel->getActiveSheet()->getDefaultStyle()->getFont()->setSize(10);
    $objPHPExcel->getActiveSheet()->getStyle('A2:E2')->getFont()->setBold(true);

    $objPHPExcel->getActiveSheet()->getStyle('A2:E2')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
    $objPHPExcel->getActiveSheet()->getStyle('A2:E2')->getBorders()->getAllBorders()->setBorderStyle(\PHPExcel_Style_Border::BORDER_THIN);

    //设置水平居中
    $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
    $objPHPExcel->getActiveSheet()->getStyle('A')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
    $objPHPExcel->getActiveSheet()->getStyle('B')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
    $objPHPExcel->getActiveSheet()->getStyle('D')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
    $objPHPExcel->getActiveSheet()->getStyle('E')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);

    //合并cell
    $objPHPExcel->getActiveSheet()->mergeCells('A1:D1');

    // set table header content
    $objPHPExcel->setActiveSheetIndex(0)
        ->setCellValue('A1', '用户汇总时间:'.date('Y-m-d H:i:s'))
        ->setCellValue('A2', '用户名')
        ->setCellValue('B2', '手机')
        ->setCellValue('C2', '邮箱');


    // Miscellaneous glyphs, UTF-8
    for($i=0;$igetActiveSheet(0)->setCellValue('A'.($i+3), $list[$i]['user']);
        $objPHPExcel->getActiveSheet(0)->setCellValue('B'.($i+3), $list[$i]['mobile']);
        $objPHPExcel->getActiveSheet(0)->setCellValue('C'.($i+3), $list[$i]['email']);
        //判断用户状态
        if($list[$i]['status']==0){    //禁用
            //改变字体颜色为红色
            $objPHPExcel->getActiveSheet()->getStyle('A'.($i+3).':C'.($i+3))->getFont()->setColor(new \PHPExcel_Style_Color(\PHPExcel_Style_Color::COLOR_RED));
            //改变背景色为黄色
            $objPHPExcel->getActiveSheet()->getStyle('A'.($i+3).':C'.($i+3))->getFill()->applyFromArray(array(
                'type' => \PHPExcel_Style_Fill::FILL_SOLID,
                'startcolor' => array(
                    'rgb' => 'FFFF00'
                )
            ));
        }
//            $objPHPExcel->getActiveSheet()->getStyle('A'.($i+3).':J'.($i+3))->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
//            $objPHPExcel->getActiveSheet()->getStyle('A'.($i+3).':J'.($i+3))->getBorders()->getAllBorders()->setBorderStyle(\PHPExcel_Style_Border::BORDER_THIN);
        $objPHPExcel->getActiveSheet()->getRowDimension($i+3)->setRowHeight(16);
    }


    //  sheet命名
    $objPHPExcel->getActiveSheet()->setTitle('用户汇总表');


    // Set active sheet index to the first sheet, so Excel opens this as the first sheet
    $objPHPExcel->setActiveSheetIndex(0);


    // excel头参数
    header('Content-Type: application/vnd.ms-excel');
    header('Content-Disposition: attachment;filename="用户汇总表('.date('Ymd-His').').xls"');  //日期为文件名后缀
    header('Cache-Control: max-age=0');

    $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');  //excel5为xls格式,excel2007为xlsx格式

    $objWriter->save('php://output');

}

4,按钮点击下载即可。

你可能感兴趣的:(php,PHPExcel)