LoginSignup
1
0

More than 5 years have passed since last update.

PhpSpreadsheetでxlsからxlsxへの変換

Posted at

前置き

エクセルからのデータ移行が必要になった :thinking:

そういう時はPhpSpreadsheet :thumbsup_tone2:
https://github.com/PHPOffice/PhpSpreadsheet

移行が必要なエクセルは実はxlsとxlsxの2パターン存在していた :ghost:
そういえばxlsの対応していないことに気づく・・・ :angel:

取り込みのロジックそのままだと、エラーになる部分があッたので、
xlsは先にxlsx変換することにした。

いざ変換


use PhpOffice\PhpSpreadsheet\Writer\Xlsx as XlsxWriter;
use PhpOffice\PhpSpreadsheet\Reader\Xls as XlsReader;

// XlsReaderで読み込み
$reader = new XlsReader();
$spreadsheet = $reader->load($xlsFilePath);

$xlsxFilePath = str_replace('xls', 'xlsx', $xlsFilePath);

// XlsxWriterで書き込み
$writer = new XlsxWriter($spreadsheet);
$writer->save($xlsxFilePath);

xls用の処理作り直そうかと一瞬迷ったけど、変換したら一瞬で解決した :raised_hands_tone2:

1
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0