SHARE
TWEET

Untitled

a guest Jan 12th, 2017 66 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. // composer require phpoffice/phpexcel
  3. require "vendor/autoload.php";
  4.  
  5. // Memory CacheだとFatal Errorが発生するため、"/tmp"にFile Cacheとして保存する
  6. $cacheSettings = array("dir" => "/tmp");
  7. $cacheMethod = PHPExcel_CachedObjectStorageFactory::cache_to_phpTemp;
  8. PHPExcel_Settings::setCacheStorageMethod($cacheMethod, $cacheSettings);
  9.  
  10. // Excelファイル読み込み
  11. $reader = PHPExcel_IOFactory::createReader("Excel2007");
  12. $book   = $reader->load("foo.xlsx");
  13.  
  14. // getSheetNames()ですべてのシート名を配列として取得
  15. foreach ($book->getSheetNames() as $sheetName) {
  16.     // シート名からレコードを取得
  17.     $sheet = $book->getSheetByName($sheetName);
  18.     $items = $sheet->toArray(null, false, true, true);
  19.  
  20.     foreach ($items as $item) {
  21.         // 余分な空配列を除去
  22.         if ($item = array_filter($item)) {
  23.             $records[$sheetName][] = $item;
  24.         }
  25.     }
  26. }
  27.  
  28. // JSONファイルとして保存
  29. $fp = fopen("bar.json", "a") or dir("Can't open file");
  30. fwrite($fp, json_encode($records, JSON_PRETTY_PRINT));
  31. fclose($fp);
RAW Paste Data
Pastebin PRO Summer Special!
Get 40% OFF on Pastebin PRO accounts!
Top