php - CSV windows excel 特殊符号编码
问题描述
我在 CSV 文件中显示特殊字符时遇到问题。它工作正常,除了在 windows excel 中。我尝试添加 windows-1252 编码,我猜它应该正确显示特殊字符,但事实并非如此。谁能告诉我我错过了什么?我的代码是:
$file = tempnam(sys_get_temp_dir(), 'mycsv');
$fh = fopen($file, 'w');
$currencyOption = $account->team->user->getOption('accounting', 'currency');
$currency = config('currencies.'.$currencyOption);
if($currency)
{
$symbol = '€';//$currency['format'];
$symbol = str_replace('%s ', '', $symbol);
$symbol = str_replace(' %s', '', $symbol);
$encoding = mb_detect_encoding($symbol);
$symbol = iconv($encoding, "Windows-1252", $symbol);
}
$groups = [];
foreach($account->fees as $fee)
{
if(!isset($groups[$fee->group]))
{
$groups[$fee->group] = $fee->name . " ($symbol)";
}
}
fputcsv($fh, array_merge([___('Player')], $groups, [___('Total') . " ($symbol)"]));
foreach($account->players as $player) {
$row = [$player->name];
foreach($groups as $groupId => $groupName)
{
$val = $account->fees->where('player_id', $player->id)->where('group', $groupId)->first();
$row[] = $val ? $val->value : '0';
}
$row[] = $player->credit;
fputcsv($fh, $row);
}
$row = [''];
foreach($groups as $groupId => $groupName)
{
$row[] = '';
}
$row[] = $account->balance;
fputcsv($fh, $row);
fclose($fh);
return response()->download($file, 'account.csv', [
'Content-Encoding' => 'Windows-1252',
'Content-Type' => 'text/csv; charset=Windows-1252',
'Content-Disposition' => "attachment; filename='account.csv'",
]);
解决方案
推荐阅读
- sql-server - 计算日期 1 和 2 之间的 datediff()
- excel - 根据其他 2 列中的条件对列中的几行求和
- java - Gradle:如何检查项目中的重复类?
- python - 导入模块以相对于导入它的文件进行文件操作
- sql - 从另一个表中选择 MAX(OrderDate) 上的 UnitCost
- javascript - Redux 中的选择器 - React 组件了解 redux 状态意味着什么?
- angular - 将 FormArray 与来自响应和 mat-checkbox 的对象一起使用
- c++ - 如何释放为指针变量本身提供的内存?
- function - 如何使函数内计算的根可用于函数外
- regex - 脚本中的命令