导出图片到excel
1 篇文章0 订阅
订阅专栏
由于PHPExcel已经被废弃已经无法获取更新,所以建议优先使用phpspreadsheet
1、composer安装(全局安装composer教程)
composer require phpoffice/phpspreadsheet
1
或者在composer.json文件require下面加上 “phpoffice/phpspreadsheet”: “^1.2”, ,之后在终端 composer update 就可以安装了
2、开始前小知识点
2.1 基础常用
use PhpOffice\PhpSpreadsheet\IOFactory; // 必须引入
use PhpOffice\PhpSpreadsheet\Spreadsheet; // 必须引入
$newExcel = new Spreadsheet(); // 实例化
// 将默认字体设置为Arial
$newExcel->getDefaultStyle()->getFont()->setName('微软雅黑');
// 将字体大小设置为12
$newExcel->getDefaultStyle()->getFont()->setSize(12);
// 将A7至B7两单元格设置为粗体字,Arial字体
$newExcel->getActiveSheet()->getStyle('A7:B7')->getFont()->setBold(true)->setName('Arial')->setSize(10);
// 将A列单元格设置为粗体字
$newExcel->getActiveSheet()->getStyle('A')->getFont()->setBold(true);
// 将A列单元格文字颜色设置为红色
$newExcel->getActiveSheet()->getStyle('A')->getFont()->getColor()->setARGB(\PhpOffice\PhpSpreadsheet\Style\Color::COLOR_RED);
// 将A列宽度设置为50
$newExcel->getActiveSheet()->getColumnDimension('A')->setWidth(50);
// 如果需要自动计算列宽,可以这样
$newExcel->getActiveSheet()->getColumnDimension('A')->setAutoSize(true);
// 设置默认列宽为50,作用于所有列
$newExcel->getActiveSheet()->getDefaultColumnDimension()->setWidth(50);
// 设置第5行行高为50
$newExcel->getActiveSheet()->getRowDimension('5')->setRowHeight(50);
// 设置默认行高为60,作用于所有行
$newExcel->getActiveSheet()->getDefaultRowDimension()->setRowHeight(60);
// 给A到F列单元格设置水平居中对齐
$newExcel->getActiveSheet()->getStyle('A:F')->getAlignment()->setVertical('center');
// 给A到F列单元格设置垂直居中对齐
$newExcel->getActiveSheet()->getStyle('A:F')->getAlignment()->setHorizontal('center');
// 单独给A列单元格设置垂直居中对齐
$newExcel->getActiveSheet()->getStyle('A')->getAlignment()->setHorizontal('center');
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
2.2 拓展小知识
2.2.1 将B2至B8的区域添加红色边框
$styleArray = [
‘borders’ => [
‘outline’ => [
‘borderStyle’ => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THICK,
‘color’ => [‘argb’ => ‘FFFF0000’]
],
]
];
$newExcel->getActiveSheet()->getStyle('B2:B8')->applyFromArray($styleArray);
1
2
3
4
5
6
7
8
9
10
2.2.2 将A1单元格设置为水平居中对齐
$styleArray = [
‘alignment’ => [
‘horizontal’ => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER,
],
];
$newExcel->getActiveSheet()->getStyle('A1')->applyFromArray($styleArray);
# 也可以对A1到C1单元格做单独处理,这里要注意的是样式覆盖,如果你上面做的是垂直居中,那这里就会把上面的覆盖掉
$newExcel->getActiveSheet()->getStyle('A1:C1')->applyFromArray($styleArray);
1
2
3
4
5
6
7
8
9
10
2.2.3 对A1到A5单元格添加背景填充色
$newExcel->getActiveSheet()->getStyle(‘A1:A5’)->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)->getStartColor()->setARGB(‘ffffff’);
1
3、以上知识点对于一般的导出基本够用了,接下来我们引入类库正式开始
# PhpSpreadsheet导出图片
public static function export()
{
set_time_limit(0);
// 读取数据
$orderList = Db::name("goods_order")->where([])->limit(3)->select();
$newExcel = new Spreadsheet(); //创建一个新的excel文档
$objSheet = $newExcel->getActiveSheet(); //获取当前操作sheet的对象
$objSheet->setTitle('订单表'); //设置当前sheet的标题
// 将默认字体设置为Arial
$newExcel->getDefaultStyle()->getFont()->setName('微软雅黑');
// 将字体大小设置为12
$newExcel->getDefaultStyle()->getFont()->setSize(12);
// 将D列单元格设置为粗体字
$newExcel->getActiveSheet()->getStyle('H')->getFont()->setBold(true);
// 将D列单元格文字颜色设置为红色
$newExcel->getActiveSheet()->getStyle('H')->getFont()->getColor()->setARGB(Color::COLOR_RED);
// 设置默认行高为60
$newExcel->getActiveSheet()->getDefaultRowDimension()->setRowHeight(60);
// 给标题单元格设置水平垂直居中对齐
$newExcel->getActiveSheet()->getStyle('A:K')->getAlignment()->setHorizontal('center');
$newExcel->getActiveSheet()->getStyle('A:K')->getAlignment()->setVertical('center');
// 给每列单元格设置宽度
$newExcel->getActiveSheet()->getColumnDimension('A')->setWidth(15);
$newExcel->getActiveSheet()->getColumnDimension('B')->setWidth(30);
$newExcel->getActiveSheet()->getColumnDimension('C')->setWidth(15);
$newExcel->getActiveSheet()->getColumnDimension('D')->setWidth(15);
$newExcel->getActiveSheet()->getColumnDimension('E')->setAutoSize(true);
$newExcel->getActiveSheet()->getColumnDimension('F')->setWidth(15);
$newExcel->getActiveSheet()->getColumnDimension('G')->setWidth(15);
$newExcel->getActiveSheet()->getColumnDimension('H')->setWidth(30);
$newExcel->getActiveSheet()->getColumnDimension('I')->setAutoSize(true);
$newExcel->getActiveSheet()->getColumnDimension('J')->setWidth(30);
$newExcel->getActiveSheet()->getColumnDimension('K')->setWidth(50);
//设置第一栏的标题
$objSheet->setCellValue('A1', '用户id')
->setCellValue('B1', '订单编号')
->setCellValue('C1', '商品分类')
->setCellValue('D1', '商品品牌')
->setCellValue('E1', '尺寸说明')
->setCellValue('F1', '订单状态')
->setCellValue('G1', '回收进度')
->setCellValue('H1', '操作说明')
->setCellValue('I1', '下单时间')
->setCellValue('J1', '正面图')
->setCellValue('K1', '备用多图');
// 第二行起,每一行的值,setCellValueExplicit是用来导出文本格式的
foreach ($orderList as $key => $val) {
$key = $key + 2;
$objSheet->setCellValue('A' . $key, $val['user_id'])
->setCellValue('B' . $key, $val['order_sn'])
->setCellValue('C' . $key, $val['category_ids'])
->setCellValue('D' . $key, $val['brand_id'])
->setCellValue('E' . $key, $val['dimensions'])
->setCellValue('F' . $key, $val['status'])
->setCellValue('G' . $key, $val['process'])
->setCellValue('H' . $key, $val['memo'])
->setCellValue('I' . $key, date('Y-m-d H:i:s', $val['createtime']));
$num = 10;
// 多图导出
if (!empty($val['images'])) {
$images = explode(",", $val['images']);
foreach ($images as $k => $v){
$image = request()->domain() . $v;
// 获取本地文件夹路径
$dir = ROOT_PATH . '/public/uploads/' . date('Ymd', time()) . '/';
$file_info = pathinfo($image);
// 过滤非文件类型
if (!empty($file_info['basename'])) {
$basename = $file_info['basename'];
// 进行检测文件是否存在
is_dir($dir) OR mkdir($dir, 0777, true);
file_put_contents($dir . $basename, file_get_contents($image));
// 引入操作图片类
$drawings[$key] = new Drawing();
$drawings[$key]->setResizeProportional(false); // TODO 此处顺序不可调,因为导出默认是按原图像缩放的,设置成false才可以设置成可控制的宽度,要注意哦!
$drawings[$key]->setName('备用多图');
$drawings[$key]->setDescription('备用多图');
$drawings[$key]->setPath($dir . $basename);
$drawings[$key]->setWidth(60);
$drawings[$key]->setHeight(60);
$drawings[$key]->setOffsetX($num);
$drawings[$key]->setOffsetY(10);
$drawings[$key]->setCoordinates('K' . $key);
$drawings[$key]->setWorksheet($objSheet);
}
$num = $num + 70; // 增加每张图之间的间距
}
}
else
{
$objSheet->setCellValue('K' . $key, '');
}
// 单图导出
if (!empty($val['positive_image'])) {
$positive_image = request()->domain() . $val['positive_image'];
// 获取本地文件夹路径
$dir = ROOT_PATH . '/public/uploads/' . date('Ymd', time()) . '/';
$file_info = pathinfo($positive_image);
// 过滤非文件类型
if (!empty($file_info['basename'])) {
$basename = $file_info['basename'];
// 进行检测文件是否存在
is_dir($dir) OR mkdir($dir, 0777, true);
file_put_contents($dir . $basename, file_get_contents($positive_image));
// 引入操作图片类
$drawing = new Drawing();
$drawing->setResizeProportional(false); // TODO 此处顺序不可调,因为导出默认是按原图像缩放的,设置成false才可以设置成可控制的宽度,要注意哦!
$drawing->setName('正面图');
$drawing->setDescription('正面图');
$drawing->setPath($dir . $basename);
$drawing->setWidth(60);
$drawing->setHeight(60);
$drawing->setOffsetX(10);
$drawing->setOffsetY(10);
$drawing->setCoordinates('J' . $key);
$drawing->setWorksheet($objSheet);
}
}
else
{
$objSheet->setCellValue('J' . $key, '');
}
}
return self::downloadExcel($newExcel, '订单表', 'Xls');
}
# 公共文件,用来传入xls并下载
public static function downloadExcel($newExcel, $filename, $format)
{
// $format只能为 Xlsx 或 Xls
if ($format == 'Xlsx') {
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
} elseif ($format == 'Xls') {
header('Content-Type: application/vnd.ms-excel');
}
header("Content-Disposition: attachment;filename=" . $filename . date('Y-m-d') . '.' . strtolower($format));
header('Cache-Control: max-age=0');
$objWriter = IOFactory::createWriter($newExcel, $format);
$objWriter->save('php://output');
//通过php保存在本地的时候需要用到
//$objWriter->save($dir . '/demo.xlsx');
// 以下为需要用到IE时候设置
// If you're serving to IE 9, then the following may be needed
// header('Cache-Control: max-age=1');
// If you're serving to IE over SSL, then the following may be needed
// header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
// header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified
// header('Cache-Control: cache, must-revalidate'); // HTTP/1.1
// header('Pragma: public'); // HTTP/1.0
return true;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
4、踩坑分享
首先我说明一下可能是我太菜了,我是第一次用PhpSpreadsheet导出图片到excel
// 引入操作图片类
d
r
a
w
i
n
g
s
[
drawings[
drawings[key] = new Drawing();
d
r
a
w
i
n
g
s
[
drawings[
drawings[key]->setResizeProportional(false);
d
r
a
w
i
n
g
s
[
drawings[
drawings[key]->setName(‘备用多图’);
d
r
a
w
i
n
g
s
[
drawings[
drawings[key]->setDescription(‘备用多图’);
d
r
a
w
i
n
g
s
[
drawings[
drawings[key]->setPath($dir . $basename);
d
r
a
w
i
n
g
s
[
drawings[
drawings[key]->setWidth(60);
d
r
a
w
i
n
g
s
[
drawings[
drawings[key]->setHeight(60);
d
r
a
w
i
n
g
s
[
drawings[
drawings[key]->setOffsetX($num);
d
r
a
w
i
n
g
s
[
drawings[
drawings[key]->setOffsetY(10);
d
r
a
w
i
n
g
s
[
drawings[
drawings[key]->setCoordinates(‘K’ . $key);
d
r
a
w
i
n
g
s
[
drawings[
drawings[key]->setWorksheet($objSheet);
1
2
3
4
5
6
7
8
9
10
11
12
因为导出图片默认是按原图像成比例缩放的,这里需要把 setResizeProportional() 设置成false才可以设置成可控制的宽度,而且设置的位置要在顶部,否则不起作用,要注意了
d
r
a
w
i
n
g
s
[
drawings[
drawings[key]->setResizeProportional(false); 这一行代码放的位置,千万不能放错了
感慨一下,我希望我们都能有一点奉献精神,就遇到问题踩的坑大家都能仔细的分享出来,与其你给我完整的demo不如给我分享你踩的坑,因为这次我没踩过这个坑不代表我下一次不会踩坑或许是因为项目需求的问题反正方方面面了,就是大家搞技术的都能奉献一点,这样很多问题都能得到解决,我就是随心写的,如果对你的心理造成伤害我表示抱歉哈,嘿嘿。
————————————————
版权声明:本文为CSDN博主「霍文霆」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_43588185/article/details/115910669