1.需求
把数据库的数据输出excel格式
2.解决方案
利用phpexcel中的examples的01和07,对excel文件的读写
3.操作流程
a.https://github.com/PHPOffice/PHPExcel下载
b.写文件
require_once '/Classes/PHPExcel.php'; $objPHPExcel = new PHPExcel(); $objPHPExcel->setActiveSheetIndex(0) ->setCellValue('A1', 'Hello') ->setCellValue('B2', 'world!') ->setCellValue('C1', 'Hello') ->setCellValue('D2', 'world!'); $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007'); $objWriter->save(str_replace('.php', '.xlsx', __FILE__));
c.读文件再写入
require_once '/Classes/PHPExcel.php'; $objPHPExcel = PHPExcel_IOFactory::load("05featuredemo.xlsx");; $objPHPExcel->setActiveSheetIndex(0) ->setCellValue('A1', 'Hello') ->setCellValue('B2', 'world!') ->setCellValue('C1', 'Hello') ->setCellValue('D2', 'world!'); $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007'); $objWriter->save(str_replace('.php', '.xlsx', __FILE__));
d.随机生成电话号码
require_once '/Classes/PHPExcel.php'; $objPHPExcel = PHPExcel_IOFactory::load("import.xls");; //13开头的 for($i=2;$i<10000;$i++) { $mobile ='13'.mt_rand(0,9).mt_rand(0,9).mt_rand(0,9).mt_rand(0,9).mt_rand(0,9).mt_rand(0,9).mt_rand(0,9).mt_rand(0,9).mt_rand(0,9); $objPHPExcel->setActiveSheetIndex(0) ->setCellValue('F'.$i, $mobile) ->setCellValue('A'.$i, $mobile); } $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007'); $objWriter->save(str_replace('.php', '.xlsx', __FILE__));
e php读取excel
http://blog.csdn.net/sadfishsc/article/details/7353722
4.总结
php对excel的操作还有其他的,遇到会慢慢记录在这。
来源:https://www.cnblogs.com/norm/p/6220308.html