Skip to content

Commit

Permalink
Merge pull request #167 from aminevg/main
Browse files Browse the repository at this point in the history
Add useEncoding to SimpleExcelReader
  • Loading branch information
freekmurze authored Apr 3, 2024
2 parents 45ac178 + 081d505 commit 256d710
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/SimpleExcelReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,15 @@ public function useFieldEnclosure(string $fieldEnclosure): self
return $this;
}

public function useEncoding(string $encoding): self
{
if ($this->reader instanceof CSVReader) {
$this->csvOptions->ENCODING = $encoding;
}

return $this;
}

public function trimHeaderRow(string $characters = null): self
{
$this->trimHeader = true;
Expand Down
11 changes: 11 additions & 0 deletions tests/SimpleExcelReaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -607,3 +607,14 @@ function () {
->fromSheetName("sheetNotExists")
->getHeaders();
})->throws(InvalidArgumentException::class);

it('can use a custom encoding', function () {
$rows = SimpleExcelReader::create(getStubPath('shift-jis-encoding.csv'))
->useEncoding('SHIFT_JIS')
->getRows()
->toArray();

expect($rows)->toEqual([
['お名前' => '太郎', 'お名前(フリガナ)' => 'タロウ'],
]);
});
2 changes: 2 additions & 0 deletions tests/stubs/shift-jis-encoding.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
�����O,�����O�i�t���K�i�j
���Y,�^���E

0 comments on commit 256d710

Please sign in to comment.