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
  | public function export_xls2($dir, $name, $data, $fields, $coms = null, $colstyle = null, $sheetname = null)
{
	// 文件名和文件类型
	$path = "/var/www/html/downloads/{$dir}/";
	if (!is_dir($path))
		mkdir($path);
	$filename = "{$path}/{$name}.xlsx";
	$filetype = "xlsx";
	$obj = new PHPExcel();
	// 以下内容是excel文件的信息描述信息
	$obj->getProperties()->setCreator(''); //设置创建者
	$obj->getProperties()->setLastModifiedBy(''); //设置修改者
	$obj->getProperties()->setTitle(''); //设置标题
	$obj->getProperties()->setSubject(''); //设置主题
	$obj->getProperties()->setDescription(''); //设置描述
	$obj->getProperties()->setKeywords('');//设置关键词
	$obj->getProperties()->setCategory('');//设置类型
	// 设置当前sheet
	$obj->setActiveSheetIndex(0);
	$sheet = $obj->getActiveSheet();
	// 设置当前sheet的名称
	if ($sheetname != null)
		$sheet->setTitle($sheetname);
	// 列标
	$list = array();
	for ($i = 0; $i < count($fields); $i ++) {
		$A = (int)($i / 26);
		$B = (int)($i % 26);
		$L = ($A > 0) ?  chr($A+64).chr($B+65) : chr($B+65);
		array_push($list, $L);
		$field = $fields[$i];
		$title = isset($coms[$i]) ? $coms[$i] : ((in_array('bm', $fields) and $field == 'zgxz') ? 'lb' : $field);
		// 填充第一行数据
		$sheet->setCellValue($L . '1', $title);
		if ($colstyle == null)
			$sheet->getStyle($L)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
		else {
		//    if ($colstyle[$i] == '0')
		//        $sheet->getStyle($L)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_NUMBER);
			if ($colstyle[$i] == '1')
				$sheet->getStyle($L)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
		}
	}
/*
	// 设置列宽
	$sheet->getColumnDimension('A')->setWidth(40);
	$sheet->getColumnDimension('B')->setWidth(10);
*/
	$n = 1;
	foreach ($data as $row) {
		$n = $n + 1;
		for ($i = 0; $i < count($fields); $i ++) {
			$field = $fields[$i];
			if ($colstyle == null)
				$sheet->setCellValueExplicit($list[$i] . ($n), $row[$field]); // PHPExcel_Cell_DataType::TYPE_STRING);
			else {
				if ($colstyle[$i] == '0')
					$sheet->setCellValue($list[$i] . ($n), $row[$field]); //, PHPExcel_Cell_DataType::TYPE_NUMERIC);
				if ($colstyle[$i] == '1')
					$sheet->setCellValueExplicit($list[$i] . ($n), $row[$field], PHPExcel_Cell_DataType::TYPE_STRING);
			}
		}
	}
	// 导出
	ob_clean();
	if ($filetype == 'xls') {
		/*
		header('Content-Type: application/vnd.ms-excel');
		header('Content-Disposition: attachment;filename="' . $filename);
		header('Cache-Control: max-age=1');
		$objWriter = new PHPExcel_Writer_Excel5($obj);
		$objWriter->save('php://output');
		*/
		$objWriter = new PHPExcel_Writer_Excel5($obj);
		$objWriter->save($filename);
	} elseif ($filetype == 'xlsx') {
		/*
		header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
		header('Content-Disposition: attachment;filename="' . $filename);
		header('Cache-Control: max-age=1');
		$objWriter = PHPExcel_IOFactory::createWriter($obj, 'Excel2007');
		$objWriter->save('php://output');
		*/
		$objWriter = PHPExcel_IOFactory::createWriter($obj, 'Excel2007');
		$objWriter->save($filename);
	}
	return ['filename' => $filename, 'row' => $n-1, 'col' => count($fields), 'size' => filesize($filename)];
}
  |