PhpSpreadsheet icon indicating copy to clipboard operation
PhpSpreadsheet copied to clipboard

If the content of the csv file contains Chinese characters enclosed in double quotes, the data will not be output correctly

Open xfstu opened this issue 1 year ago • 4 comments

This is:

- [X] a bug report
- [ ] a feature request
- [ ] **not** a usage question (ask them on https://stackoverflow.com/questions/tagged/phpspreadsheet or https://gitter.im/PHPOffice/PhpSpreadsheet)

What is the expected behavior?

Read the csv correctly

What is the current behavior?

The csv cannot be read correctly

What are the steps to reproduce?

Please provide a Minimal, Complete, and Verifiable example of code that exhibits the issue without relying on an external Excel file or a web server:

test.csv:

"test","2","中文","4"
<?php

include __DIR__ . '/../library/vendor/autoload.php';

use PhpOffice\PhpSpreadsheet\IOFactory;

$filePath = './test.csv'; 

$spreadsheet = IOFactory::load($filePath);
$worksheet = $spreadsheet->getActiveSheet();

foreach ($worksheet->getRowIterator() as $row) {
  $cellIterator = $row->getCellIterator();
  $cellIterator->setIterateOnlyExistingCells(false);

  $rowData = [];
  foreach ($cellIterator as $cell) {
    $rowData[] = $cell->getValue();
  }
  print_r($rowData);
}
?>

//output:
Array
(
    [0] => test
    [1] => 2
    [2] => 中文",4"
)
//expect:
Array
(
    [0] => test
    [1] => 2
    [2] => 中文
    [3] => 4
)

At present, my solution is not to include double quotes in the content of csv files, but I hope you can deal with it inside the source code. Thank you very much. Sorry, my English is not very good.

If this is an issue with reading a specific spreadsheet file, then it may be appropriate to provide a sample file that demonstrates the problem; but please keep it as small as possible, and sanitize any confidential information before uploading.

What features do you think are causing the issue

  • [x] Reader
  • [ ] Writer
  • [ ] Styles
  • [ ] Data Validations
  • [ ] Formula Calculations
  • [ ] Charts
  • [ ] AutoFilter
  • [ ] Form Elements

Does an issue affect all spreadsheet file formats? If not, which formats are affected?

So far I've only found this problem in csv files

Which versions of PhpSpreadsheet and PHP are affected?

The PhpSpreadsheet version is ^1.29 and the PHP version is 7.4

xfstu avatar Mar 15 '24 06:03 xfstu

I am not able to duplicate your result - I see the expected result when I execute your code. I know you have shown the contents of test.csv above. Please upload the file instead and I will try to see if there is something about the file that causes your unexpected result.

oleibman avatar Mar 15 '24 16:03 oleibman

I am not able to duplicate your result - I see the expected result when I execute your code. I know you have shown the contents of test.csv above. Please upload the file instead and I will try to see if there is something about the file that causes your unexpected result.

@oleibman Thank you for taking the time to help me look at this problem.My running environment is: PHP 7.4, Windows Simplified Chinese version, version number 10 22H2 test-false.csv

xfstu avatar Mar 16 '24 15:03 xfstu

Sorry, still getting the expected result (Php7.4 PhpSpreadsheet 1.0.29 Windows 11 US, and also more recent releases of Php and PhpSpreadsheet):

Array
(
    [0] => test
    [1] => 中文
    [2] => 1
)

I don't have any further ideas on why you are seeing a problem on your system.

oleibman avatar Mar 17 '24 04:03 oleibman

Sorry, still getting the expected result (Php7.4 PhpSpreadsheet 1.0.29 Windows 11 US, and also more recent releases of Php and PhpSpreadsheet):

Array
(
    [0] => test
    [1] => 中文
    [2] => 1
)

I don't have any further ideas on why you are seeing a problem on your system.

@oleibman Thanks for your reply, I tried to set up a Linux system in VMware, ran the example code in Linux, It did output normally. Now preliminary judgment may be Windows 10 simplified Chinese version of the coding problem. Thank you again for your help.

xfstu avatar Mar 18 '24 14:03 xfstu

Problem seems resolved as far as PhpSpreadsheet is concerned. Closing.

oleibman avatar Aug 07 '24 03:08 oleibman