类库使用 gitHub项目
说明:用PHP导出数据库数据形成Excel表格,并且下载,在网上能找到很多这样的资料,所以我也就不再多说了,我留下我整理的代码吧,反正就是这么用,代码只适用ThinkPHP。
public function export(){
import("Org.Util.PHPExcel");
import("Org.Util.PHPExcel.Writer.Excel2007");
import("Org.Util.PHPExcel.IOFactory.php");
$objPHPExcel = new \PHPExcel();
$objPHPExcel->getProperties();
$objPHPExcel->setActiveSheetIndex(0);
$objActSheet = $objPHPExcel->getActiveSheet();
$objActSheet->mergeCells('A1:B1');
$objStyleA1 = $objActSheet->getStyle('A1');
$objAlignA1 = $objStyleA1->getAlignment();
$objAlignA1->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objActSheet->setCellValue('A1', "礼品包-{$package['name']}");
$objActSheet->setCellValue('A2', "礼品包名称");
$objActSheet->setCellValue('A3', "礼品包适用Server");
$objActSheet->setCellValue('A4', "礼品包生成时间");
$objActSheet->setCellValue('A5', "礼品包开始时间");
$objActSheet->setCellValue('A6', "礼品包结束时间");
$objActSheet->setCellValue('A7', "数据包导出时间");
$objActSheet->setCellValue('A8', "礼品包内容");
$objActSheet->setCellValue('B2', $package['name']);
$objActSheet->setCellValue('B3', $package['serverId']);
$objActSheet->setCellValue('B4', date('Y-m-d H:i:s', $package['addTime']));
$objActSheet->setCellValue('B5', ( $package['startTime']?date('Y-m-d H:i:s', $package['startTime']):"立即开始") );
$objActSheet->setCellValue('B6', ( $package['endTime']?date('Y-m-d H:i:s', $package['endTime']):"永久生效") );
$objActSheet->setCellValue('B7', date('Y-m-d H:i:s'));
$packageArr = json_decode($package['contents'], true);
$packageInfo = '';
foreach( $packageArr as $value ){
$packageInfo .= "{$value['configID']}:{$value['num']}|";
}
$packageInfo = rtrim($packageInfo,'|');
$objActSheet->setCellValue('B8', $packageInfo);
$column = 9;
foreach($keys as $rows){
$objActSheet->mergeCells('A'.$column.':B'.$column);
$objActSheet->setCellValue('A'.$column, $rows['key']);
$column++;
}
$fileName = "礼品包-{$package['name']}.xlsx";
$fileName = iconv("utf-8", "gb2312", $fileName);
ob_end_clean();
header('Content-Type: application/vnd.ms-excel');
header("Content-Disposition: attachment;filename=\"$fileName\"");
header('Cache-Control: max-age=0');
$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
exit;
}
本文由 陌上花开 创作,采用 知识共享署名4.0 国际许可协议进行许可
本站文章除注明转载/出处外,均为本站原创或翻译,转载前请务必署名
最后编辑时间为: Jul 1, 2016 at 04:08 am