Export.php
2.1 KB
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
<?php
namespace addons\shopro\library;
use EasyWeChat\Factory;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
/**
*
*/
class Export
{
public function __construct()
{
}
public function exportExcel($expTitle, $expCellName, $expTableData){
$fileName = $expTitle;
$cellNum = count($expCellName);
$dataNum = count($expTableData);
$spreadsheet = new Spreadsheet();
$cellName = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'AA', 'AB', 'AC', 'AD', 'AE', 'AF', 'AG', 'AH', 'AI', 'AJ', 'AK', 'AL', 'AM', 'AN', 'AO', 'AP', 'AQ', 'AR', 'AS', 'AT', 'AU', 'AV', 'AW', 'AX', 'AY', 'AZ');
$sheet = $spreadsheet->getActiveSheet(0);
// for ($i = 0; $i < $cellNum; $i++) {
// $sheet->getColumnDimension($cellName[$i])->setWidth(30);
// }
$sheet->getStyle('A1:' . $cellName[$cellNum - 1] . '1')->getFont()->setBold(true);
$i = 0;
foreach ($expCellName as $key => $cell) {
$sheet->setCellValue($cellName[$i] . '1', $cell);
$i ++;
}
for ($i = 0; $i < $dataNum; $i++) {
if ($i == ($dataNum - 1)) {
$sheet->mergeCells('A' . ($i + 2) . ':' . $cellName[$cellNum - 1] . ($i + 2));
$sheet->setCellValue('A' . ($i + 2), $expTableData[$i][key($expCellName)]);
} else {
$j = 0;
foreach ($expCellName as $key => $cell) {
$sheet->setCellValue($cellName[$j] . ($i + 2), $expTableData[$i][$key]);
$j++;
}
}
}
header('pragma:public');
header('Content-type:application/vnd.ms-excel;charset=utf-8;name="' . $fileName . '.xls"');
header("Content-Disposition:attachment;filename=$fileName.xls"); //attachment新窗口打印inline本窗口打印
$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
$writer->save('php://output');
}
}