2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

PerlでBOM付UTF-8のCSV(Excelで直接読めるCSV)を出力する方法

Last updated at Posted at 2023-08-18

Excelで読み取れるCSV

文字コード UTF-8 のCSVファイルをExcelで開くと、以下のように文字化けする。
image.png

文字化けをさせないためには先頭にBOM(バイトオーダーマーク)を付ける必要がある。

#PerlでBOMを出力するサンプル

bom.pl
#!/usr/bin/perl
use strict;
use warnings;

# BOMを出力する
print "\x{EF}\x{BB}\x{BF}";

# CSVを出力する
print "ID,名前"."\n";
print "1,山田"."\n";
print "2,田中"."\n";
print "3,坂本"."\n";

実行

./bom.pl > excel.csv

とするとExcelで読み込めるCSVファイル(excel.csv)ができる。
image.png

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?