phpexcel下载多个sheet表单

$objPHPExcel = new PHPExcel();

默认一个sheet

$objPHPExcel->getProperties()->setCreator("test")
->setLastModifiedBy("test")
->setTitle("Office 2007 XLSX Test Document")
->setSubject("Office 2007 XLSX Test Document")
->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
->setKeywords("office 2007 openxml php")
->setCategory("Test result file");


$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A1', 'sku')
->setCellValue('B1', 'price')
->setCellValue('C1', 'current_price');

//循环数组输出

for($i=0;$i<count($result);$i++)
{
$objPHPExcel->setActiveSheetIndex(0)->setCellValue('A'.($i+2), $result[$i]['sku']);
$objPHPExcel->setActiveSheetIndex(0)->setCellValue('B'.($i+2), $result[$i]['price']);
$objPHPExcel->setActiveSheetIndex(0)->setCellValue('C'.($i+2), $result[$i]['current_price']);
}
$objPHPExcel->getActiveSheet()->setTitle('title');

$objPHPExcel->setActiveSheetIndex(0);

 

如果添加一个sheet工作簿

//创建sheet

$objPHPExcel->createSheet();

$objPHPExcel->getSheet(1)->setTitle('imgurl-IE');


$objPHPExcel->setActiveSheetIndex(1)
->setCellValue('A1', 'sku')
->setCellValue('B1', 'url');

for($i=0;$i<count($imgarr);$i++)
{
$objPHPExcel->setActiveSheetIndex(1)->setCellValue('A'.($i+2), $imgarr[$i]['sku']);
$objPHPExcel->setActiveSheetIndex(1)->setCellValue('B'.($i+2), $imgarr[$i]['imgurl']);
}

//设置工作簿名称
$objPHPExcel->getActiveSheet()->setTitle('imgurl');


$objPHPExcel->setActiveSheetIndex(1);

////////////////////////////////////////////////////////////////////////////////

 

header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="shangxin'.date("Y-m-d").'.xls"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
exit;

 

posted @ 2015-08-11 09:09  Forward1990  阅读(824)  评论(0编辑  收藏  举报