Skip to content

Commit

Permalink
gbk
Browse files Browse the repository at this point in the history
  • Loading branch information
Ethan committed Apr 28, 2020
1 parent eb82bb0 commit 3e9392c
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 14 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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');
```
61 changes: 50 additions & 11 deletions src/Excel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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) {

Expand All @@ -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;
}

}
3 changes: 2 additions & 1 deletion test/4.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');

0 comments on commit 3e9392c

Please sign in to comment.