如何在 laravel 中导出 shift-jis 编码的 csv 文件?
How to export csv file with shift-jis encoding in laravel?
我正在使用 laravel-excel 导出 csv 文件。要导出,代码如下,
return Excel::download(new Export(results,$header), "test.csv");
Export.php 文件就像,
namespace App\AllClass;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\WithHeadings;
class Export implements FromCollection,WithHeadings
{
private $myArray;
private $header;
public function __construct($myArray,$header){
$this->myArray = $myArray;
$this->header = $header;
}
public function collection()
{
$data = mb_convert_encoding($this->myArray,"SJIS", "UTF-8");
// dump($data);
return collect($data);
}
public function headings(): array
{
$header = mb_convert_encoding($this->header,"SJIS", "UTF-8");
// dump($header);
return $header;
}
}
如您所见,我在创建 excel 之前转换数据。无需转换,我可以完美导出为 UTF-8 格式。但在转换为 shift-jis 后,它正在删除所有日文字符。但是,如果我在返回之前转储 header,它会向我显示乱码数据;不像 csv 文件那样为空字符串。
我解决了。
让我在这里分享我的解决方案。
Laravel Excel 默认不支持。
但是我们可以通过简单的方式做到。
下载前获取csv内容:\Excel::raw
转换为另一种编码:mb_convert_encoding
https://docs.laravel-excel.com/3.1/exports/collection.html#storing-raw-contents
下载 csv.
$exportedObject= new \App\Exports\ClassExport($exportDataArray, $fileName);
$csvContent = \Excel::raw($exportedObject, $exportedObject->writerType);
$csvContent = mb_convert_encoding($csvContent, 'SJIS', 'auto');
// 就我而言,我将我的 csv 上传到 S3。
$storageInstance = \Storage::disk('s3_import_csvs');
$putFileOnStorage = $storageInstance->put($fileName, $csvContent);
在 config/excel.php 中,您应该更改 CSV 设置
'use_bom' => true,
它的日语效果很好
我正在使用 laravel-excel 导出 csv 文件。要导出,代码如下,
return Excel::download(new Export(results,$header), "test.csv");
Export.php 文件就像,
namespace App\AllClass;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\WithHeadings;
class Export implements FromCollection,WithHeadings
{
private $myArray;
private $header;
public function __construct($myArray,$header){
$this->myArray = $myArray;
$this->header = $header;
}
public function collection()
{
$data = mb_convert_encoding($this->myArray,"SJIS", "UTF-8");
// dump($data);
return collect($data);
}
public function headings(): array
{
$header = mb_convert_encoding($this->header,"SJIS", "UTF-8");
// dump($header);
return $header;
}
}
如您所见,我在创建 excel 之前转换数据。无需转换,我可以完美导出为 UTF-8 格式。但在转换为 shift-jis 后,它正在删除所有日文字符。但是,如果我在返回之前转储 header,它会向我显示乱码数据;不像 csv 文件那样为空字符串。
我解决了。
让我在这里分享我的解决方案。
Laravel Excel 默认不支持。
但是我们可以通过简单的方式做到。
下载前获取csv内容:\Excel::raw
转换为另一种编码:mb_convert_encoding https://docs.laravel-excel.com/3.1/exports/collection.html#storing-raw-contents
下载 csv.
$exportedObject= new \App\Exports\ClassExport($exportDataArray, $fileName);
$csvContent = \Excel::raw($exportedObject, $exportedObject->writerType);$csvContent = mb_convert_encoding($csvContent, 'SJIS', 'auto');
// 就我而言,我将我的 csv 上传到 S3。 $storageInstance = \Storage::disk('s3_import_csvs');
$putFileOnStorage = $storageInstance->put($fileName, $csvContent);
在 config/excel.php 中,您应该更改 CSV 设置
'use_bom' => true,
它的日语效果很好