IT源码网

php的Excel相关操作

xmjava 2021年02月16日 编程语言 469 0

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的操作还有其他的,遇到会慢慢记录在这。

 

评论关闭
IT源码网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!