Skip to content

add ability to set codepage explicitly for BIFF5 #1484

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

Merged
merged 4 commits into from
Jun 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).
- Add support for IFS() logical function [#1442](https://github.com/PHPOffice/PhpSpreadsheet/pull/1442)
- Add Cell Address Helper to provide conversions between the R1C1 and A1 address formats [#1558](https://github.com/PHPOffice/PhpSpreadsheet/pull/1558)
- Add ability to edit Html/Pdf before saving [#1499](https://github.com/PHPOffice/PhpSpreadsheet/pull/1499)
- Add ability to set codepage explicitly for BIFF5 [#1018](https://github.com/PHPOffice/PhpSpreadsheet/issues/1018)

### Fixed

Expand Down
11 changes: 10 additions & 1 deletion src/PhpSpreadsheet/Reader/Xls.php
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,15 @@ public function canRead($pFilename)
}
}

public function setCodepage(string $codepage): void
{
if (!CodePage::validate($codepage)) {
throw new PhpSpreadsheetException('Unknown codepage: ' . $codepage);
}

$this->codepage = $codepage;
}

/**
* Reads names of the worksheets from a file, without parsing the whole file to a PhpSpreadsheet object.
*
Expand Down Expand Up @@ -640,7 +649,7 @@ public function load($pFilename)

// initialize
$this->pos = 0;
$this->codepage = 'CP1252';
$this->codepage = $this->codepage ?: CodePage::DEFAULT_CODE_PAGE;
$this->formats = [];
$this->objFonts = [];
$this->palette = [];
Expand Down
7 changes: 7 additions & 0 deletions src/PhpSpreadsheet/Shared/CodePage.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

class CodePage
{
public const DEFAULT_CODE_PAGE = 'CP1252';

private static $pageArray = [
0 => 'CP1252', // CodePage is not always correctly set when the xls file was saved by Apple's Numbers program
367 => 'ASCII', // ASCII
Expand Down Expand Up @@ -65,6 +67,11 @@ class CodePage
65001 => 'UTF-8', // Unicode (UTF-8)
];

public static function validate(string $codePage): bool
{
return in_array($codePage, self::$pageArray, true);
}

/**
* Convert Microsoft Code Page Identifier to Code Page Name which iconv
* and mbstring understands.
Expand Down