之前书写的一个导出图片的实例,每次翻之前的代码都直接写成日志,。
通过数据库中的查询数组后输出
//下载方法
public function downloadExcel()
{
require 'Lib/PHPExcel/Classes/PHPExcel.php';
$data //$data为获取到的数据库中的数据
error_reporting(E_ALL);
//北京时区
date_default_timezone_set('PRC');
$objPHPExcel = new \PHPExcel();
$objDrawing = new \PHPExcel_Worksheet_Drawing();
/*以下是一些设置作者 标题啊之类的*/
$objPHPExcel->getProperties()->setCreator("nkbh")
->setLastModifiedBy("nkbh")
->setTitle("nkbh")
->setSubject("nkbh")
->setDescription("详细描述")
->setKeywords("excel")
->setCategory("result file");
/*以下就是对处理Excel里的数据*/
$objPHPExcel->setActiveSheetIndex(0)
//Excel的第1列,数据表头
->setCellValue('A' . 1, "ID")
->setCellValue('B' . 1, "名称")
->setCellValue('C' . 1, "图样")
->setCellValue('D' . 1, "类别")
//循环输出表中数据
foreach ($data as $k => $v) {
$num = $k + 2;
$objPHPExcel->setActiveSheetIndex(0)
//Excel的第N列,数据格式化
->setCellValue('A' . $num, $v['id'])
->setCellValue('B' . $num, $v['name'])
// ->setCellValue('C' . $num, $v['src'])
->setCellValue('D' . $num, $v['type'])
$objDrawing->setName('Paid');
$objDrawing->setDescription('Paid');
$objDrawing->setPath('./Public/Uploads/' . $v['src']); //图片引入位置
$objDrawing->setCoordinates('C'.$num); //图片添加位置
$objDrawing->setOffsetX(210);
$objDrawing->setRotation(25);
$objDrawing->setHeight(36);
$objDrawing->getShadow()->setVisible(true);
$objDrawing->getShadow()->setDirection(45);
}
$objDrawing->setWorksheet($objPHPExcel->getActiveSheet()->setTitle('User'));
$objPHPExcel->setActiveSheetIndex(0);
header('Content-Type: application/vnd.ms-excel');
//使用当前时间戳作为文件名
header('Content-Disposition: attachment;filename="' . time() . '.xls"');
header('Cache-Control: max-age=0');
$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
exit;
}