Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // composer require phpoffice/phpexcel
- require "vendor/autoload.php";
- // Memory CacheだとFatal Errorが発生するため、"/tmp"にFile Cacheとして保存する
- $cacheSettings = array("dir" => "/tmp");
- $cacheMethod = PHPExcel_CachedObjectStorageFactory::cache_to_phpTemp;
- PHPExcel_Settings::setCacheStorageMethod($cacheMethod, $cacheSettings);
- // Excelファイル読み込み
- $reader = PHPExcel_IOFactory::createReader("Excel2007");
- $book = $reader->load("foo.xlsx");
- // getSheetNames()ですべてのシート名を配列として取得
- foreach ($book->getSheetNames() as $sheetName) {
- // シート名からレコードを取得
- $sheet = $book->getSheetByName($sheetName);
- $items = $sheet->toArray(null, false, true, true);
- foreach ($items as $item) {
- // 余分な空配列を除去
- if ($item = array_filter($item)) {
- $records[$sheetName][] = $item;
- }
- }
- }
- // JSONファイルとして保存
- $fp = fopen("bar.json", "a") or dir("Can't open file");
- fwrite($fp, json_encode($records, JSON_PRETTY_PRINT));
- fclose($fp);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement