iliaal / php_excel

PHP Extension interface to the Excel writing/reading library
http://ilia.ws
533 stars 131 forks source link

problem with multibyte sheet name #247

Closed james-little closed 3 years ago

james-little commented 5 years ago

hi

I got a excel file which has a worksheet with a Japanese sheet name. I tried to get the worksheet by the sheet name, but the getSheetByName function gave me a false return. then i tried to get the worksheet by index, then wanted to check the string of the sheet name, the name function returned me a NULL result.

Am i wrong in some point?

Code: $excel_file = _2utf8('C:\Users\knx.kok\Desktop\mail_パラメータシート\test.xlsx');

if (!is_file($excel_file)) { echo 'file not exist'; exit ; }

$xlsx_book = new ExcelBook(null, null, true); $xlsx_book->setLocale('Shift-JIS'); $xlsx_book->loadFile($excel_file); $contents_sheet = $xlsx_book->getSheet(1); // $contents_sheet = $xlsx_book->getSheetByName('Sheet1'); var_dump($contents_sheet->name());

function _2utf8($str) { return mb_convert_encoding($str, 'Shift-JIS', 'UTF-8');
}

Environment: Win7 32bit (Japanese Edition) php: 5.6

※ at first i thought it was some bugs of the libxl, then i upgraded libxl to the newest version. but the result didn't change.

Please give me a help. Thank you very much.

johmue commented 5 years ago

@james-little have you tried the following and what did you receive?

$xlsx_book = new \ExcelBook(null, null, true);
$xlsx_book->setLocale('UTF-8'); // no Shift-JIS
$xlsx_book->loadFile($excel_file);

$contents_sheet = $xlsx_book->getSheet(0);
var_dump($contents_sheet->name());
fischel commented 5 years ago

If the code of @johmue is not working, try to set the locale like this:

$this->setLocale('ja_JP.utf8');

And you have to to check if the locale is available on your server with:

locale -a

Output can look like this: C C.UTF-8 de_DE.utf8 en_AG en_AG.utf8 en_AU.utf8 en_BW.utf8 en_CA.utf8 en_DK.utf8 en_GB.utf8 en_HK.utf8 en_IE.utf8 en_IL en_IL.utf8 en_IN en_IN.utf8 en_NG en_NG.utf8 en_NZ.utf8 en_PH.utf8 en_SG.utf8 en_US.utf8 en_ZA.utf8 en_ZM en_ZM.utf8 en_ZW.utf8 POSIX Here you have to find ja_JP.utf8. If not:

https://askubuntu.com/questions/76013/how-do-i-add-locale-to-ubuntu-server

james-little commented 5 years ago

@johmue

It worked! but i don't understand, what does this locale means? cause i worked with a japanese windows 7 environment, the default encoding is Shift-JIS, so i set it to Shift-JIS. why it should be UTF-8?

Thank you very much!

james-little commented 5 years ago

@fischel

hi fischel, Thanks for your comment.

I worked with a windows environment so the command is like chcp. what i dont understand is the system default encoding is Shift-JIS, but why i should set it to UTF-8. the php internal encoding is utf-8, is that means the locale should be the same as php internal encoding? but the excel file is encoded with Shift-JIS...

Thank you very much!