Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
1 of 8 tasks
xfstu opened this issue Mar 15, 2024 · 4 comments

Comments

@xfstu
Copy link

xfstu commented Mar 15, 2024

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

  • 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

@oleibman
Copy link
Collaborator

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.

@xfstu
Copy link
Author

xfstu commented Mar 16, 2024

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

@oleibman
Copy link
Collaborator

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.

@xfstu
Copy link
Author

xfstu commented Mar 18, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants