读xlsx转换为数组

时间:2015-04-23 08:33:23

标签: php arrays excel

是否可以使用PHP读取以下xlsx文件,然后将其转换为这样的数组?

我想我需要一个支持坐标的Excel类。

img

array("Date" => "05-jan", "Room" => "205", "Activity" => "");
array("Date" => "05-jan", "Room" => "209", "Activity" => "Test1");

array("Date" => "06-jan", "Room" => "205", "Activity" => "");
array("Date" => "06-jan", "Room" => "205", "Activity" => "Test2");

array("Date" => "07-jan", "Room" => "205", "Activity" => "");
array("Date" => "07-jan", "Room" => "209-pc", "Activity" => "Test3");

array("Date" => "08-jan", "Room" => "205", "Activity" => "");
array("Date" => "08-jan", "Room" => "209-pc", "Activity" => "");

array("Date" => "09-jan", "Room" => "205", "Activity" => "");
array("Date" => "09-jan", "Room" => "209-pc", "Activity" => "");

使用此代码我获取内容。

https://github.com/nuovo/spreadsheet-reader

require('php-excel-reader/excel_reader2.php');
require('SpreadsheetReader.php');
$Reader = new SpreadsheetReader('2015.xlsx');
 $Sheets = $Reader -> Sheets();


foreach ($Sheets as $Index => $Name)
{

    $Reader -> ChangeSheet($Index);

    foreach ($Reader as $Row)
    {
        print_r($Row);
    }
}

输出

Array ( [0] => [1] => [2] => 2 - 2015 [3] => [4] => [5] => [6] => )
Array ( [0] => [1] => [2] => 5-Jan [3] => 6-Jan [4] => 7-Jan [5] => 8-Jan[6] => 9-Jan ) 
  Array ( [0] => [1] => [2] => Monday [3] => Tuesday [4] => Wednesday [5] => Thursday [6] => Friday ) 
   Array ( [0] => 205 [1] => Fo. [2] => [3] => [4] => [5] => [6] => ) 
  Array ( [0] => 24+ [1] => Ef. [2] => [3] => [4] => [5] => [6] => ) 
   Array ( [0] => 209-pc [1] => Fo. [2] => Test1 [3] => Test2 [4] => Test3 [5] => [6] => ) 
Array ( [0] => 24 [1] => Ef. [2] => [3] => [4] => [5] => [6] => ) 

2 个答案:

答案 0 :(得分:0)

您可以使用PHP Excel Reader

这会根据需要将您的Excel数据存储为数组。非常方便。

答案 1 :(得分:0)

我当前的SpreadsheetReader版本也存在问题。

您可以通过更改SpreadsheetReader_XLSX.php文件中的Sheets()方法来解决此问题

      public function Sheets() {

      if ($this -> Sheets === false) {
          $this -> Sheets = array();
          foreach ($this -> WorkbookXML -> sheets -> sheet as $Index => $Sheet) {
              $AttributesWithPrefix = $Sheet -> attributes('r', true);
              $Attributes = $Sheet -> attributes();
              $rId = 0;
              $sheetId = 0;
              foreach ($AttributesWithPrefix as $Name => $Value) {
                  if ($Name == 'id') {
                     $rId = (int)str_replace('rId', '', (string)$Value);
                     break;
                  }
              }

              foreach ($Attributes as $Name => $Value) {
                  if ($Name == 'sheetId') {
                      $sheetId = (int)$Value;
                      break;
                  }
              }
              $this -> Sheets[min($rId, $sheetId)] = (string)$Sheet['name'];
          }
          ksort($this -> Sheets);
      } 
      return array_values($this -> Sheets);
  }