PHPExcel导出导入

时间:2024-11-12 19:34:02

便于记忆 这里写一个PHPexcel导出的demo

我们构造一个数据

 
        $letter = array('A','B','D','E'); //sheet索引
 
        $date = array(
                array(
                    'ID',
                    '姓名',
                    '性别',
                    '职务'
                ),
                array(
                    12,
                    '方言-1',
                    '男',
                    'developer'
                ),
                array(
                    13,
                    '方言-2',
                    '男',
                    'developer'
                ),
                array(
                    14,
                    '方言-3',
                    '男',
                    'developer'
                ),
                array(
                    15,
                    '方言-4',
                    '男',
                    'developer'
                ));//需要写入的数据
            
 
        $xls_name = date('Y-m-d',time());//命名excel


这里是导出excel文件的方法:

    /**
     * @param $letter 索引
     * @param $date sheet数据
     * @param $xls_name 导出文件名
     */
    private function _export_action($letter, $date, $xls_name)
    {
        require_once(ROOT_PATH.'/includes/libraries/PHPExcel.php');
        require_once(ROOT_PATH.'/includes/libraries/PHPExcel/IOFactory.php');
        require_once(ROOT_PATH.'/includes/libraries/PHPExcel/Writer/IWriter.php');
        require_once(ROOT_PATH.'/includes/libraries/PHPExcel/Writer/Excel5.php');
        set_time_limit(0);// 取消超时限制
        $excel = new PHPExcel();
        if($letter && $date)
        {
            $i=1;
            foreach($date as $key => $val)
            {
                $sheet = $excel->setActiveSheetIndex(0);
                $j = 0;
                foreach($val as $k => $v)
                {
                    $index = $letter[$j].$i;
                    $excel->setActiveSheetIndex(0)->setCellValue($index,$v);
                    if($i == 1)
                    {
                        $sheet->getStyle($index)->getFont()->setSize(10);//单元格的字体大小
                        $sheet->getStyle($index)->getFont()->setBold(true);//单元格字体加粗
                        if($letter[$j] == 'A')
                        {
                            $sheet->getStyle($index)->getFont()->getColor()->setRGB('FF0000');
                        }
                    }
                    $sheet->getStyle($index)->getFont()->setName('微软雅黑');
                    $j++;
                }
                $i++;
            }
        }
 
        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        header('Content-Disposition: attachment;filename="'.$xls_name.'.xlsx"');
        header('Cache-Control: max-age=0');
        $writer=PHPExcel_IOFactory::createWriter($excel,'Excel2007');
        //Excel5-->office 97-2003
        //Excel2007  -->office2007 2010 ..
        $writer->save('php://output');exit;
    }

这里是导出结果:我的索引是 A B D E 。

PHPExcel导出导入

导入:是对sheet内容挨个数据读取,比较简单;这里就不写方法了;

参考:

http://www.cnblogs.com/jackluo/archive/2013/04/15/3021599.html

http://www.cnblogs.com/jackluo/archive/2013/04/09/3010277.html

PHPExcel下载