From 3e9392c0883235d50137d8265885b852858abbc6 Mon Sep 17 00:00:00 2001 From: Ethan <190196539@qq.com> Date: Tue, 28 Apr 2020 12:10:05 +0800 Subject: [PATCH] gbk --- README.md | 4 ++-- src/Excel.php | 61 +++++++++++++++++++++++++++++++++++++++++---------- test/4.php | 3 ++- 3 files changed, 54 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 08293b9..8e1ee6a 100644 --- a/README.md +++ b/README.md @@ -74,13 +74,13 @@ Excel::exportExcel($data, $map, $file, '用户信息'); //Excel::toExcelFile($data, $map, $file, '用户信息'); -//分块导出到CSV文件 +//分块导出到CSV文件 (如果中文乱码,输出为GBK字符集,将UTF-8改为GBK即可) Excel::chunkExportCSV($map, './temp.csv', function ($writer) { DB::select('user')->orderBy('id')->chunk(100, function ($users) use ($writer) { /** \Closure $writer */ $writer($users); }); -}); +}, 'UTF-8'); ``` diff --git a/src/Excel.php b/src/Excel.php index 1551595..392a3e2 100755 --- a/src/Excel.php +++ b/src/Excel.php @@ -471,19 +471,21 @@ public static function convertTime($excelTime, $format = 'Y-m-d H:i:s') * * }); * - * @param array $map - * $map = array( - * 'id' => '编号', - * 'name' => '姓名', - * 'age' => '年龄', - * ); * - * @param \Closure $cb + * @param array $map + * $map = array( + * 'title'=>array( + * 'id' => '编号', + * 'name' => '姓名', + * 'age' => '年龄', + * ) + * ) * @param null $filename + * @param \Closure $cb + * @param string $charset * @return string - * @throws \Exception */ - public static function chunkExportCSV($map, $filename, $cb) + public static function chunkExportCSV($map, $filename, $cb, $charset = 'UTF-8') { if (empty($filename)) { $filename = uniqid(); @@ -496,14 +498,18 @@ public static function chunkExportCSV($map, $filename, $cb) $fp = fopen($filename, 'w'); if (empty($map['title'])) { - throw new \Exception('$map miss title key'); + throw new \RuntimeException('$map miss title key'); } + $map = Excel::convertUtf8ToOther($map, $charset); + fputcsv($fp, $map['title']); $fields = array_keys($map['title']); - $cb(function ($arr) use ($fp, $fields) { + $cb(function ($arr) use ($fp, $fields, $charset) { + + $arr = Excel::convertUtf8ToOther($arr, $charset); foreach ($arr as $val) { @@ -521,4 +527,37 @@ public static function chunkExportCSV($map, $filename, $cb) return $filename; } + + /** + * 将UTF-8的数据,转为指定的编码 + * @param array $data + * @param $charset + * @return array + */ + private static function convertUtf8ToOther($data, $charset) + { + if (strtolower($charset) === 'utf-8') { + return $data; + } + + if (is_string($data)) { + return @iconv("UTF-8", $charset, $data); + } + + $result = array(); + foreach ($data as $k => $v) { + $k = @iconv("UTF-8", $charset, $k); + + if (is_array($v)) { + $v = self::convertUtf8ToOther($v, $charset); + } else { + $v = @iconv("UTF-8", $charset, $v); + } + + $result[$k] = $v; + } + + return $result; + } + } \ No newline at end of file diff --git a/test/4.php b/test/4.php index 2d1c438..204b0aa 100644 --- a/test/4.php +++ b/test/4.php @@ -23,8 +23,9 @@ array('id' => 3, 'name' => '"Ethan', 'age' => 34), array('id' => 4, 'name' => '\'Tony', 'age' => 34), array('id' => 5, 'name' => ',', 'age' => 34), + array('id' => 6, 'name' => '张三', 'age' => 34), ); /** \Closure $writer */ $writer($data); -}); +}, 'gbk');