//安装phpexcel的composer包
composer require phpoffice/phpexcel
<?php
namespace app\index\controller;
use think\Controller;
use PHPExcel_Worksheet_Drawing;
use PHPExcel;
use PHPExcel_IOFactory;
use PHPExcel_Style_Alignment;
use think\facade\Env;
class Index extends Controller
{
public function index(){
$root = Env::get('root_path');
$path = $root.'public/static/images/avatar.png';
$arr = [
[
'id' => 1,
'username' => 'wyq',
'nickname' => '心如止水',
'password' => '123456',
'phone' => '13451167651',
'init_time' => 1622632002,
'img' => $path
],
[
'id' => 2,
'username' => 'fj',
'nickname' => 'xj',
'password' => '123456',
'phone' => '13451163651',
'init_time' => 1622632002,
'img' => $path
],
[
'id' => 3,
'username' => 'szy',
'nickname' => 'szy',
'password' => '123456',
'phone' => '13451163641',
'init_time' => 1622632002,
'img' => $path
]
];
//实例化
$objExcel = new PHPExcel();
//设置文档属性
$objWriter = PHPExcel_IOFactory::createWriter($objExcel, 'Excel2007');
//设置内容;
$objActSheet = $objExcel->getActiveSheet();
$objActSheet->getStyle("A:Z")
->getAlignment()
->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER)
->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
$key = ord("A");
$letter = explode(',', "A,B,C,D,E,F,G");
//设置表头
$arrHeader = array('用户ID', '用户名', '昵称','用户密码','手机号码' ,'注册时间','图片');
$lenth = count($arrHeader);
//填充表头信息 A1:用户ID、B1:用户名、C1:昵称
for ($i = 0; $i < $lenth; $i++) {
$objActSheet->setCellValue("$letter[$i]1", "$arrHeader[$i]");
};
//填充表格信息
foreach ($arr as $k => $v) {
//从第二行开始
$k += 2;
//表格内容
$objActSheet->setCellValue('A' . $k, $v['id']);
$objActSheet->setCellValue('B' . $k, $v['username']);
$objActSheet->setCellValue('C' . $k, $v['nickname']);
$objActSheet->setCellValue('D' . $k, $v['password']);
$objActSheet->setCellValue('E' . $k, $v['phone']);
$objActSheet->setCellValue('F' . $k, date('Y-m-d H:i:s', $v['init_time']));
if ($v['img']){
//实例化图片操作类
$objDrawing = new PHPExcel_Worksheet_Drawing();
//设置图片地址
$objDrawing -> setPath($v['img']);
//设置图片高
$objDrawing ->setHeight(30);
//设置图片宽
$objDrawing ->setWidth(30);
//设置图片存放在表格的位置
$objDrawing ->setCoordinates('G' . $k);
//设置X方向偏移量
$objDrawing ->setOffsetX(12);
//设置Y方向偏移量
$objDrawing ->setOffsetY(12);
$objDrawing ->setWorksheet($objActSheet);
//设置表格的高度
$objActSheet->getRowDimension($k)->setRowHeight(50);
}
$width = array(20, 20, 15, 10, 10, 30, 10, 15);
//设置表格的宽度
$objActSheet->getColumnDimension('A')->setWidth(10);
$objActSheet->getColumnDimension('B')->setWidth(20);
$objActSheet->getColumnDimension('C')->setWidth(20);
$objActSheet->getColumnDimension('D')->setWidth(30);
$objActSheet->getColumnDimension('E')->setWidth(20);
$objActSheet->getColumnDimension('F')->setWidth(20);
$objActSheet->getColumnDimension('G')->setWidth(10);
}
$outfile = "人员表" . time() . ".xlsx";
//清空输出缓冲区
ob_end_clean();
//告诉浏览器强制下载
header("Content-Type: application/force-download");
//二进制文件类型
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
//设置表名
header('Content-Disposition:inline;filename="' . $outfile . '"');
header("Content-Transfer-Encoding: binary");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Pragma: no-cache");
$objWriter->save('php://output');
}
}
TinkPHP5.1使用phpexcel导出excel包含图片问题解决方案
最新推荐文章于 2023-11-06 14:16:20 发布