0

I am using PhpSpreadsheet to read files (csv, xls, xlsx). But when I reading csv files, the content of Chinese word can't read (it's null). enter image description here

This csv file: enter image description here

This is my code:

<?php
  require 'vendor/autoload.php';
  use PhpOffice\PhpSpreadsheet\Spreadsheet;
  use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
  use PhpOffice\PhpSpreadsheet\Reader\Xls;
  use PhpOffice\PhpSpreadsheet\Reader\Csv;
  $inputFileName = 'input1.csv';
  //load files
  $spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($inputFileName);
  //change the file to array
  $sheetData = $spreadsheet->getActiveSheet()->toArray(null,true,true,true);
  print_r($sheetData);
?>

I had try setlocale(LC_ALL, 'zh_TW.UTF-8 UTF-8'); but it didn't work.

Piao
  • 1
  • 3

2 Answers2

0

You almost certainly need to tell PHPSpreadsheet what encoding is used for the csv file using the csv reader's setInputEncoding() method

Mark Baker
  • 209,507
  • 32
  • 346
  • 385
0

If it is a csv file, you could try the following codes:

$reader = new \PhpOffice\PhpSpreadsheet\Reader\Csv();
$reader->setInputEncoding('GBK'); //this line would internally convert chinese gbk to utf-8 encoding
$spreadsheet = $reader->load($excelFile);
$data = $spreadsheet->getActiveSheet()->toArray(null, true, true, true);
iefreer
  • 189
  • 3
  • 6