-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinput.php
132 lines (111 loc) · 3.71 KB
/
input.php
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
<?php
require_once('Classes/PHPExcel.php');
require_once('Classes/PHPExcel/Writer/Excel2007.php');
require_once('Classes/PHPExcel/IOFactory.php');
//设置缓存
$cacheMethod = PHPExcel_CachedObjectStorageFactory:: cache_to_phpTemp;
$cacheSettings = array(' memoryCacheSize ' => '8MB');
PHPExcel_Settings::setCacheStorageMethod($cacheMethod, $cacheSettings);
//列单元
$b1 = 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');
$b2 = array();
for ($i = 0; $i < count($b1); $i++) {
for ($j = 0; $j < count($b1); $j++) {
$b2[] = $b1[$i] . $b1[$j];
}
}
$b = array_merge($b1, $b2);
//表1
$headarr = array();
$bodyarr = array();
$filename = './uploads/a.xls';
$objPHPExcelReader = PHPExcel_IOFactory::load($filename); //加载excel文件
foreach ($objPHPExcelReader->getWorksheetIterator() as $sheet) //循环读取sheet
{
$i = 0;
foreach ($sheet->getRowIterator() as $row) //逐行处理
{
$j = 0;
//获取标题字段
if ($row->getRowIndex() < 2) //确定从哪一行开始读取
{
foreach ($row->getCellIterator() as $cell) //逐列读取
{
$head = $cell->getValue(); //获取cell中数据
$headarr[0][] = $head;
}
continue;
}
foreach ($row->getCellIterator() as $cell) //逐列读取
{
$data = $cell->getValue(); //获取cell中数据
$bodyarr[$i][$j] = $data;
$j += 1;
}
$i+=1;
}
}
//表2
$headarr2 = array();
$bodyarr2 = array();
$filename = './uploads/b.xls';
$objPHPExcelReader = PHPExcel_IOFactory::load($filename); //加载excel文件
foreach ($objPHPExcelReader->getWorksheetIterator() as $sheet) //循环读取sheet
{
$i = 0;
foreach ($sheet->getRowIterator() as $row) //逐行处理
{
$j = 0;
//获取标题字段
if ($row->getRowIndex() < 2) //确定从哪一行开始读取
{
foreach ($row->getCellIterator() as $cell) //逐列读取
{
$head2 = $cell->getValue(); //获取cell中数据
$headarr2[0][] = $head2;
}
continue;
}
foreach ($row->getCellIterator() as $cell) //逐列读取
{
$data2 = $cell->getValue(); //获取cell中数据
$bodyarr2[$i][$j] = $data2;
$j += 1;
}
$i+=1;
}
}
//合并数组
$result = array_merge($headarr, $bodyarr, $bodyarr2);
$objPHPExcel = new PHPExcel();
//标题
$objPHPExcel->getProperties()->setTitle("php合并excel");
//获取列数
$column_number=count($result[0]);
//设置列字段名
for($i=0;$i<$column_number;$i++){
$objPHPExcel->getActiveSheet()->setCellValue($b[$i].'1', $result[0][$i]);
}
$i = 1;
foreach ($result as $row) {
for($j=0;$j<$column_number;$j++){
$objPHPExcel->getActiveSheet()->setCellValue("$b[$j]$i", $result[$i][$j]);
}
$i += 1;
}
//清除乱码
ob_end_clean();
// 输出Excel表格到浏览器下载
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="abc.xls"');
header('Cache-Control: max-age=0');
// 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
$objWriter = new PHPExcel_Writer_Excel5($objPHPExcel);
$objWriter->save('php://output');
?>