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
<?php
/**
* CsvFormat
* データをCSVとして出力する時のクラスです。
* $Id: CsvFormat.inc,v 1.1 2015/10/08 11:18:56 wanggb Exp $
* @author iimuro
* @access public
* @package jp.aimslib2.io
*/
/*
* CSV出力の基本処理
* ,を含む値→""で囲む
* "を含む値→""に変換
*/
class CsvFormat{
/**
* @var array 出力するデータがはいった、2次元配列。
*/
public $data = null;
public $file_path = null;
public $file_name = null;
public $delimiter = ",";
/**
* @public boolean SJISへの文字コード変換を行うか?trueの場合、行う。
*/
public $convert_encoding = true;
/**
* @var String 出力時の文字コード。デフォルトで、Shift_JIS。
*/
public $output_encoding = "Shift_JIS";
/**
* @var String 入力されたデータの文字コード。autoの場合、自動判別を行う。
*/
public $data_encoding = "auto";
/**
* データをCSV形式で出力したり、保存したりしたいときに利用するクラスです。<br />
* 2次元配列でデータをセットします。<br />
*/
public function __construct($data = null) {
if ($data != null) {
if (is_array($data)) {
$this->data = $data;
}
}
}
/**
* 出力時のファイル名をセットします。
*/
public function setFileName($name) {
$this->file_name = $name;
}
/**
* ファイルとして保存する場合のディレクトリをセットします。
*/
public function setFilePath($path) {
$this->file_path = $path;
}
/**
* 出力時に文字コード変換を行うかを設定します。
* @param boolean $convert 文字コード変換を行うか?true:する。
* @param String $output_encoding 出力するときの文字コード。
* @param String $data_encoding 入力されたデータの文字コード。autoを指定した場合、自動判別。
*/
public function setEncodingConvert($convert, $output_encoding = null, $data_encoding = null) {
$this->convert_encoding = $convert;
if ($output_encoding != null) {
$this->output_encoding = $output_encoding;
}
if ($data_encoding != null) {
$this->data_encoding = $data_encoding;
}
}
/**
* 文字列データとして返します。
*/
public function toString() {
$result = "";
foreach ($this->data as $row) {
$row_data = "";
foreach ($row as $cell) {
if ($row_data != "") {
$row_data .= $this->delimiter;
}
$row_data .= CsvFormat::doCsvEscape($cell);
}
$result .= trim($row_data) . "\n";
}
// 文字コード指定がある場合は変換してやる。
if ($this->convert_encoding) {
if ($this->data_encoding == "auto") {
$result = mb_convert_encoding($result, $this->output_encoding);
} else {
$result = mb_convert_encoding($result, $this->output_encoding, $this->data_encoding);
}
}
return $result;
}
/**
* HTML出力します。
*/
public function printOut() {
if ($this->file_name != null) {
header("Pragma: cache");
header("Content-type: application/octet-stream; name=" . $this->file_name);
header("Content-disposition: attachment; filename=" . $this->file_name);
} else {
header("Content-type: application/octet-stream;");
}
header("Content-Transfer-Encoding: binary");
print $this->toString();
}
/**
* データをCSV出力用にエスケープします。
* ,を含む値→""で囲む
* "を含む値→""に変換
*/
public function doCsvEscape($str) {
if (strpos($str, "\"") !== false) {
$str = str_replace("\"", "\"\"", $str);
}
$do_escape = false;
if (strpos($str, ",") !== false) {
$do_escape = true;
}
if (strpos($str, "\n") !== false) {
$do_escape = true;
}
if ($do_escape) {
$str = "\"" . $str . "\"";
}
return $str;
}
}
?>