/
ExcelCreator.php
62 lines (53 loc) · 2.09 KB
/
ExcelCreator.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
<?php
require_once 'PHPExcel_1.8.0/Classes/PHPExcel.php';
require_once 'PHPExcel_1.8.0/Classes/PHPExcel/IOFactory.php';
class ExcelCreator
{
private $path, $data;
// ヘッダ
private static $header = ['No', 'タイトル', '起票日', '更新日', '担当者', 'ステータス', 'ラベル', 'リリース予定'];
/**
* コンストラクタ
*/
public function __construct($data)
{
$this->path = dirname(__FILE__) . '/issues.xls';
$this->data = $data;
}
/**
* Excelにデータを出力し、ダウンロードする
*/
public function generate()
{
$excel = new PHPExcel();
$excel->setActiveSheetIndex(0);
$sheet = $excel->getActiveSheet();
$sheet->setTitle('課題管理表');
// ヘッダ設定
foreach (self::$header as $key => $val) {
$sheet->setCellValueByColumnAndRow($key, 1, $val);
}
// データ設定
foreach ($this->data as $key => $issue) {
$row = $key + 2;
$sheet->setCellValueByColumnAndRow(0, $row, $issue['No']);
$sheet->setCellValueByColumnAndRow(1, $row, $issue['Title']);
$sheet->setCellValueByColumnAndRow(2, $row, $issue['Created']);
$sheet->setCellValueByColumnAndRow(3, $row, $issue['Updated']);
$sheet->setCellValueByColumnAndRow(4, $row, $issue['Assignee']);
$sheet->setCellValueByColumnAndRow(5, $row, $issue['State']);
$sheet->setCellValueByColumnAndRow(6, $row, $issue['Labels']);
$sheet->setCellValueByColumnAndRow(7, $row, $issue['Milestone']);
}
$writer = PHPExcel_IOFactory::createWriter($excel, 'Excel2007');
$writer->save($this->path);
// 出力
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment; filename=' . basename($this->path) . ';');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($this->path));
readfile($this->path);
unlink($this->path);
exit();
}
}