Skip to content

Commit 256d710

Browse files
authored
Merge pull request #167 from aminevg/main
Add useEncoding to SimpleExcelReader
2 parents 45ac178 + 081d505 commit 256d710

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

src/SimpleExcelReader.php

+9
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,15 @@ public function useFieldEnclosure(string $fieldEnclosure): self
103103
return $this;
104104
}
105105

106+
public function useEncoding(string $encoding): self
107+
{
108+
if ($this->reader instanceof CSVReader) {
109+
$this->csvOptions->ENCODING = $encoding;
110+
}
111+
112+
return $this;
113+
}
114+
106115
public function trimHeaderRow(string $characters = null): self
107116
{
108117
$this->trimHeader = true;

tests/SimpleExcelReaderTest.php

+11
Original file line numberDiff line numberDiff line change
@@ -607,3 +607,14 @@ function () {
607607
->fromSheetName("sheetNotExists")
608608
->getHeaders();
609609
})->throws(InvalidArgumentException::class);
610+
611+
it('can use a custom encoding', function () {
612+
$rows = SimpleExcelReader::create(getStubPath('shift-jis-encoding.csv'))
613+
->useEncoding('SHIFT_JIS')
614+
->getRows()
615+
->toArray();
616+
617+
expect($rows)->toEqual([
618+
['お名前' => '太郎', 'お名前(フリガナ)' => 'タロウ'],
619+
]);
620+
});

tests/stubs/shift-jis-encoding.csv

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
�����O,�����O�i�t���K�i�j
2+
���Y,�^���E

0 commit comments

Comments
 (0)