首页 > 代码库 > php的Excel相关操作

php的Excel相关操作

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__));

4.总结

php对excel的操作还有其他的,遇到会慢慢记录在这。

 

php的Excel相关操作