PhpSpreadsheetでCSVをSJIS-winで出力する

ちょっと前に Pull Request していたんですが、今日マージして頂いたので紹介します。

PhpSpreadsheet を最新にアップデートしてください。
以下がサンプルプログラムです。

<?php
require 'C:/php/php-8.0.2-Win32-vs16-x64/vendor/autoload.php';

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Csv;

$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setCellValue('A1', 'こんにちは!');
$sheet->setCellValue('B1', 'Hello!');

$writer = new Csv($spreadsheet);
$writer->setUseBOM(false);
$writer->setOutputEncoding('SJIS-win');
$writer->save('hello.csv');

Writer/Csv に setOutputEncoding と getOutputEncoding メソッドを追加しました。setOutputEncoding で PHP の mb_convert_encoding で指定可能なエンコーディングを指定します。

複数のシートを読み込んでアプリケーションに読み込ませる CSV を作成したりする場合に便利だと思いますよ。

コメントする

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

*

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください