PHP_ThinkPHP使用phpExcel实现Excel数据的导出

作者: manito_y
发布时间:2015-06-29 18:07:43

在实习的时候,接到一个任务要求将用户搜索出来的记录导出到excel,并保存在客户端,在网上找到了PHPExcel,用了一下,感觉很强大,基本上可以设置excel的所有样式。而我也只是用到了里面的很少的功能,就是导出。

在贴出代码之前首先说明一下,设置路径是非常关键的,thinkphpphp设置路径的差别比较大,一定要多注意,不过我是现在php上测试的,然后再thinkphp框架下移植,公司要求必须在thinkphp下开发,其实thinkphp真的没有我想的那么好,虽然是方便了,但是很多限制了编程的思维,并不是很灵活。

其实导出的代码很简单,下面是我的一个例子:

                   header("charset=utf-8");

                   ini_set ('memory_limit', '128M');

                   $type=$_REQUEST['type'];

                   $search_type=$_REQUEST['search_type'];

                   $keyword=$_REQUEST['keyword'];

                   $sql_select="SELECT * FROM pp_view_planorder_prouduct WHERE type='".$type."' AND ".$search_type." LIKE '%".$keyword."%'";

                   $Model = new Model();

                   $list=$Model->query($sql_select);

                   //echo($sql_select);

 

                   error_reporting(E_ALL);

                   import("phpexcel.PHPExcel",dirname(__FILE__),".php");

                   $objPHPExcel = new PHPExcel();

                   // Set document properties

                   $objPHPExcel->getProperties()->setCreator("aigindustries")//创建者

                   ->setLastModifiedBy("aigindustries")//最后修改者

                   ->setTitle("aigindustries")//标题

                   ->setSubject("aigindustries")//主题

                   ->setDescription("aigindustries")//备注

                   ->setKeywords("aigindustries")//关键字

                   ->setCategory("aigindustries");//分类

                   // Add some data

                   $objPHPExcel->setActiveSheetIndex(0)

                   //设置表的名称标题

                   ->setCellValue('A1',"季度")

                   ->setCellValue('B1',"部门")

                   ->setCellValue('C1',"姓名")

                   ->setCellValue('D1',"备货性质")

                   ->setCellValue('E1',"组别")

                   ->setCellValue('F1',"类型")

                   ->setCellValue('G1',"品名")

                   ->setCellValue('H1',"规格")

                   ->setCellValue('I1',"材质")

                   ->setCellValue('J1',"包装")

                   ->setCellValue('K1',"季度第一个月订货量")

                   ->setCellValue('L1',"季度第二个月订货量")

                   ->setCellValue('M1',"季度第三个月订货量");

                   foreach($list as $k => $v)

                   {

                            $num=$k+2;

                            $objPHPExcel->setActiveSheetIndex(0)

                            //Excel的第A列,uid是你查出数组的键值,下面以此类推

                            ->setCellValue('A'.$num, $v['quarter'])

                            ->setCellValue('B'.$num, $v['userdept'])

                            ->setCellValue('C'.$num, $v['username'])

                            ->setCellValue('D'.$num, $v['prdfeature'])

                            ->setCellValue('E'.$num, $v['prdgroup'])

                            ->setCellValue('F'.$num, $v['prdkind'])

                            ->setCellValue('G'.$num, $v['prdname'])

                            ->setCellValue('H'.$num, $v['standard'])

                            ->setCellValue('I'.$num, $v['resource'])

                            ->setCellValue('J'.$num, $v['pack'])

                            ->setCellValue('K'.$num, $v['value1'])

                            ->setCellValue('L'.$num, $v['value2'])

                            ->setCellValue('M'.$num, $v['value3']);

                   }

                   // Rename worksheet

                   $objPHPExcel->getActiveSheet()->setTitle($type.'-该季度所有订货信息');

                   // Set active sheet index to the first sheet, so Excel opens this as the first sheet

                   $objPHPExcel->setActiveSheetIndex(0);

                   // Redirect output to a client’s web browser (Excel5)

                   header('Content-Type: application/vnd.ms-excel;charset=utf-8');

                   header('Content-Disposition: attachment;filename="'.$type.'-该季度所有订货信息.xls"');

                   header('Cache-Control: max-age=0');

                   $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');

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

                   exit;

上边的import("phpexcel.PHPExcel",dirname(__FILE__),".php");thinkphp导入的语句,我直接就将phpexcel放在了action文件夹下,网上说放什么地方,其实只要能导入都行。导入的方式一定要看thinkphp的手册,里面很详细。

我上面实现的例子就是讲数据库中查询出来的数据导出到excel表格中,你只要换成自己的查询出来的数据就可以了。

标签: Excel php thinkphp
来源:http://blog.csdn.net/manito_y/article/details/8972216

推荐: