下面是PHPExcel的导入与导出的基本操作,也是最重要的两个操作。
生成文件:
<?php
require_once './Classes/PHPExcel.php';
$content = file_get_contents('./json.txt');
$content = json_decode($content,true);
$objPHPExcel=new PHPExcel(); $objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A1','sku')
->setCellValue('B1','name')
->setCellValue('C1','catalog_name');
foreach($content as $k=>$v){$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A'.$k+2,$v['sku'])
->setCellValue('B'.$k+2,$v['name'])
->setCellValue('C'.$k+2,$v['catalog_name']);
}
$objPHPExcel->getActiveSheet()->setTitle('Product'); $objPHPExcel->setActiveSheetIndex(0);
$filename='product'; //生成xls文件
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="'.$filename.'.xls"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
exit;
读取文件:
$filename = 'D:/Source/US.xlsx';
$PHPExcel = new PHPExcel();
$objReader = PHPExcel_IOFactory::createReaderForFile($filename);
$objPHPExcel = $objReader->load($filename);
$data = array();
$objPHPExcel->setActiveSheetIndex(0);
for($j=1;$j<=60;$j++)
{$sku = $objPHPExcel->getActiveSheet()->getCell("A".$j)->getValue();
}