0
0

exampleReader13.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. error_reporting(E_ALL);
  3. set_time_limit(0);
  4. date_default_timezone_set('Europe/London');
  5. ?>
  6. <html>
  7. <head>
  8. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  9. <title>PHPExcel Reader Example #13</title>
  10. </head>
  11. <body>
  12. <h1>PHPExcel Reader Example #13</h1>
  13. <h2>Simple File Reader for Multiple CSV Files</h2>
  14. <?php
  15. /** Include path **/
  16. set_include_path(get_include_path() . PATH_SEPARATOR . '../../../Classes/');
  17. /** PHPExcel_IOFactory */
  18. include 'PHPExcel/IOFactory.php';
  19. $inputFileType = 'CSV';
  20. $inputFileNames = array('./sampleData/example1.csv','./sampleData/example2.csv');
  21. $objReader = PHPExcel_IOFactory::createReader($inputFileType);
  22. $inputFileName = array_shift($inputFileNames);
  23. echo 'Loading file ',pathinfo($inputFileName,PATHINFO_BASENAME),' into WorkSheet #1 using IOFactory with a defined reader type of ',$inputFileType,'<br />';
  24. $objPHPExcel = $objReader->load($inputFileName);
  25. $objPHPExcel->getActiveSheet()->setTitle(pathinfo($inputFileName,PATHINFO_BASENAME));
  26. foreach($inputFileNames as $sheet => $inputFileName) {
  27. echo 'Loading file ',pathinfo($inputFileName,PATHINFO_BASENAME),' into WorkSheet #',($sheet+2),' using IOFactory with a defined reader type of ',$inputFileType,'<br />';
  28. $objReader->setSheetIndex($sheet+1);
  29. $objReader->loadIntoExisting($inputFileName,$objPHPExcel);
  30. $objPHPExcel->getActiveSheet()->setTitle(pathinfo($inputFileName,PATHINFO_BASENAME));
  31. }
  32. echo '<hr />';
  33. echo $objPHPExcel->getSheetCount(),' worksheet',(($objPHPExcel->getSheetCount() == 1) ? '' : 's'),' loaded<br /><br />';
  34. $loadedSheetNames = $objPHPExcel->getSheetNames();
  35. foreach($loadedSheetNames as $sheetIndex => $loadedSheetName) {
  36. echo '<b>Worksheet #',$sheetIndex,' -> ',$loadedSheetName,'</b><br />';
  37. $objPHPExcel->setActiveSheetIndexByName($loadedSheetName);
  38. $sheetData = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
  39. var_dump($sheetData);
  40. echo '<br /><br />';
  41. }
  42. ?>
  43. <body>
  44. </html>