2010年05月15日
【Mantis】csvエクスポートをシフトJISで出力する
デフォルトの状態でcsv出力してみると、
メモ帳ではもちろん問題なく開けるのだが、
エクセル(わたくしの場合Excel2003)で開くと、
文字化け
する。
原因は、エクセルが文字コードがUTF-8のcsvを開けない仕様だから・・・
いったんメモ帳で開いて、文字コードを Shift JIS に変換した形で保存をし、
それをエクセルで開くという手間を省くために、初めから Shift JIS で出力するには
どうすればよいか?
その前にphpは超ど級の初心者なので、初心者的修正をやっているかも(汗)
でも、少なくとも私の環境では動作する・・・
※Mantisのバージョンは 1.2.0 。
(そのほかのバージョンでは通用するかよく分からない)
修正するファイル csv_export.php
80行目辺り?(いろいろいじってしまって・・・)
$t_header = ob_get_clean();
ってあると思うのだが、それよりも下の部分が修正対象。
$t_header = ob_get_clean();
# Fixed for a problem in Excel where it prompts error message "SYLK: File Format Is Not Valid"
# See Microsoft Knowledge Base Article - 323626
# http://support.microsoft.com/default.aspx?scid=kb;en-us;323626&Product=xlw
$t_first_three_chars = utf8_substr( $t_header, 0, 3 );
if ( strcmp( $t_first_three_chars, 'ID' . $t_sep ) == 0 ) {
$t_header = str_replace( 'ID' . $t_sep, 'Id' . $t_sep, $t_header );
}
# end of fix
#エクセルでcsvが開けるように文字コードをShiftJISに変更する
#echo $t_header;
echo mb_convert_encoding($t_header,'SJIS', 'UTF-8' );
# export the rows
foreach ( $t_rows as $t_row ) {
$t_first_column = true;
foreach ( $t_columns as $t_column ) {
if ( !$t_first_column ) {
#echo $t_sep;
echo mb_convert_encoding($t_sep,'SJIS', 'UTF-8' ); } else {
$t_first_column = false;
}
$t_custom_field = column_get_custom_field_name( $t_column );
if ( $t_custom_field !== null ) {
ob_start();
$t_column_value_function = 'print_column_value';
helper_call_custom_function( $t_column_value_function, array( $t_column, $t_row, COLUMNS_TARGET_CSV_PAGE ) );
$t_value = ob_get_clean();
#echo csv_escape_string($t_value);
echo mb_convert_encoding(csv_escape_string($t_value),'SJIS', 'UTF-8' );
} else {
$t_function = 'csv_format_' . $t_column;
#echo $t_function( $t_row->$t_column );
echo mb_convert_encoding($t_function( $t_row->$t_column ),'SJIS', 'UTF-8' );
}
}
#echo $t_nl;
echo mb_convert_encoding($t_nl,'SJIS', 'UTF-8' );
}
この記事がお役に立ったようでしたらぜひクリックお願いしますm(_ _)m
人気ブログランキングへ
メモ帳ではもちろん問題なく開けるのだが、
エクセル(わたくしの場合Excel2003)で開くと、
文字化け
する。
原因は、エクセルが文字コードがUTF-8のcsvを開けない仕様だから・・・
いったんメモ帳で開いて、文字コードを Shift JIS に変換した形で保存をし、
それをエクセルで開くという手間を省くために、初めから Shift JIS で出力するには
どうすればよいか?
その前にphpは超ど級の初心者なので、初心者的修正をやっているかも(汗)
でも、少なくとも私の環境では動作する・・・
※Mantisのバージョンは 1.2.0 。
(そのほかのバージョンでは通用するかよく分からない)
修正するファイル csv_export.php
80行目辺り?(いろいろいじってしまって・・・)
$t_header = ob_get_clean();
ってあると思うのだが、それよりも下の部分が修正対象。
$t_header = ob_get_clean();
# Fixed for a problem in Excel where it prompts error message "SYLK: File Format Is Not Valid"
# See Microsoft Knowledge Base Article - 323626
# http://support.microsoft.com/default.aspx?scid=kb;en-us;323626&Product=xlw
$t_first_three_chars = utf8_substr( $t_header, 0, 3 );
if ( strcmp( $t_first_three_chars, 'ID' . $t_sep ) == 0 ) {
$t_header = str_replace( 'ID' . $t_sep, 'Id' . $t_sep, $t_header );
}
# end of fix
#エクセルでcsvが開けるように文字コードをShiftJISに変更する
#echo $t_header;
echo mb_convert_encoding($t_header,'SJIS', 'UTF-8' );
# export the rows
foreach ( $t_rows as $t_row ) {
$t_first_column = true;
foreach ( $t_columns as $t_column ) {
if ( !$t_first_column ) {
#echo $t_sep;
echo mb_convert_encoding($t_sep,'SJIS', 'UTF-8' ); } else {
$t_first_column = false;
}
$t_custom_field = column_get_custom_field_name( $t_column );
if ( $t_custom_field !== null ) {
ob_start();
$t_column_value_function = 'print_column_value';
helper_call_custom_function( $t_column_value_function, array( $t_column, $t_row, COLUMNS_TARGET_CSV_PAGE ) );
$t_value = ob_get_clean();
#echo csv_escape_string($t_value);
echo mb_convert_encoding(csv_escape_string($t_value),'SJIS', 'UTF-8' );
} else {
$t_function = 'csv_format_' . $t_column;
#echo $t_function( $t_row->$t_column );
echo mb_convert_encoding($t_function( $t_row->$t_column ),'SJIS', 'UTF-8' );
}
}
#echo $t_nl;
echo mb_convert_encoding($t_nl,'SJIS', 'UTF-8' );
}
この記事がお役に立ったようでしたらぜひクリックお願いしますm(_ _)m
人気ブログランキングへ
タグ :Mantis
Mantisをインストールしたときの話【その6】
Mantisをインストールしたときの話【その5】
Mantisをインストールしたときの話【その4】
Mantisをインストールしたときの話【その3】
Mantisをインストールしたときの話【その2】
Mantisをインストールしたときの話【その1】
Mantisをインストールしたときの話【その5】
Mantisをインストールしたときの話【その4】
Mantisをインストールしたときの話【その3】
Mantisをインストールしたときの話【その2】
Mantisをインストールしたときの話【その1】
Posted by ようすけ at 00:02│Comments(0)
│Mantis