2009-02-19 74 views
82

我在嘗試讀取Excel文件(Office 2003)。有一個Excel文件需要上傳並解析其內容。在PHP中讀取Excel文件

通過Google,我只能找到這些相關(和不足的主題)的答案:生成Excel文件,讀取Excel XML文件,讀取Excel CSV文件或未完成的廢棄項目。我擁有Office 2003,所以如果我需要任何文件,它們都可用。它安裝在我的盒子上,但不是也不能安裝在我的共享主機上。

編輯:到目前爲止所有回答指向PHP-ExcelReader和/或this additional article關於如何使用它。

+1

[Zend Article](http://devzone.zend.com/article/3336-Reading-and-Writing-Spreadsheets-with-PHP) – IEnumerator 2009-02-19 01:56:04

+3

Edit上的兩個鏈接已損壞! – Houari 2016-06-06 10:10:50

回答

52

你有2個選擇,因爲據我所知:

  1. Spreadsheet_Excel_Reader,它知道Office 2003的二進制格式
  2. PHPExcel,這既知道的Office 2003和Excel 2007中(XML)。

PHPExcel將Spreadsheet_Excel_Reader用於Office 2003格式。

更新:我曾經讀過一些Excel文件,但是我使用了Office 2003 XML格式來讀取它們,並告訴正在使用該應用程序保存和上載該類型Excel文件的人員。

+1

這些都不是爲了生成而不是爲了閱讀? – Dinah 2009-02-19 02:00:01

+0

他們都允許閱讀和寫作。 – IEnumerator 2009-02-19 02:05:12

+0

@NTulip:我沒有在功能列表或文檔中看到任何暗示他們可以從Excel文件中讀取的文檔。任何例子? – Dinah 2009-02-19 02:12:36

15

這取決於您想如何使用excel文件中的數據。如果你想將它導入到mysql中,你可以簡單地將它保存爲CSV格式的文件,然後使用fgetcsv來解析它。

3

嘗試......

我用下面的代碼爲 「XLS和XLSX」

<?php 
    include 'excel_reader.php';  // include the class 
    $excel = new PhpExcelReader;  // creates object instance of the class 
    $excel->read('excel_file.xls'); // reads and stores the excel file data 

    // Test to see the excel data stored in $sheets property 
    echo '<pre>'; 
    var_export($excel->sheets); 

    echo '</pre>'; 

    or 

echo '<pre>'; 
    print_r($excel->sheets); 

    echo '</pre>'; 

參考:http://coursesweb.net/php-mysql/read-excel-file-data-php_pc

2
// Here is the simple code using COM object in PHP 
class Excel_ReadWrite{ 

    private $XLSHandle; 
    private $WrkBksHandle; 
    private $xlBook; 

    function __construct() { 
     $this->XLSHandle = new COM("excel.application") or die("ERROR: Unable to instantaniate COM!\r\n"); 
    } 

    function __destruct(){ 
     //if already existing file is opened 
     if($this->WrkBksHandle != null) 
     { 
      $this->WrkBksHandle->Close(True); 
      unset($this->WrkBksHandle); 
      $this->XLSHandle->Workbooks->Close(); 
     } 
     //if created new xls file 
     if($this->xlBook != null) 
     { 
      $this->xlBook->Close(True); 
      unset($this->xlBook); 
     } 
     //Quit Excel Application 
     $this->XLSHandle->Quit(); 
     unset($this->XLSHandle); 
    } 

    public function OpenFile($FilePath) 
    { 
     $this->WrkBksHandle = $this->XLSHandle->Workbooks->Open($FilePath); 
    } 

    public function ReadData($RowNo, $ClmNo) 
    { 
     $Value = $this->XLSHandle->ActiveSheet->Cells($RowNo, $ClmNo)->Value; 
     return $Value; 
    } 

    public function SaveOpenedFile() 
    { 
     $this->WrkBksHandle->Save(); 
    } 

    /*********************************************************************************** 
    * Function Name:- WriteToXlsFile() will write data based on row and column numbers 
    * @Param:- $CellData- cell data 
    * @Param:- $RowNumber- xlsx file row number 
    * @Param:- $ColumnNumber- xlsx file column numbers 
    ************************************************************************************/ 
    function WriteToXlsFile($CellData, $RowNumber, $ColumnNumber) 
    { 
     try{ 
       $this->XLSHandle->ActiveSheet->Cells($RowNumber,$ColumnNumber)->Value = $CellData; 
      } 
     catch(Exception $e){ 
       throw new Exception("Error:- Unable to write data to xlsx sheet"); 
      } 
    } 


    /**************************************************************************************** 
    * Function Name:- CreateXlsFileWithClmName() will initialize xls file with column Names 
    * @Param:- $XlsColumnNames- Array of columns data 
    * @Param:- $XlsColumnWidth- Array of columns width 
    *******************************************************************************************/ 
    function CreateXlsFileWithClmNameAndWidth($WorkSheetName = "Raman", $XlsColumnNames = null, $XlsColumnWidth = null) 
    { 
     //Hide MS Excel application window 
     $this->XLSHandle->Visible = 0; 
     //Create new document 
     $this->xlBook = $this->XLSHandle->Workbooks->Add(); 

     //Create Sheet 1 
     $this->xlBook->Worksheets(1)->Name = $WorkSheetName; 
     $this->xlBook->Worksheets(1)->Select; 

     if($XlsColumnWidth != null) 
     { 
      //$XlsColumnWidth = array("A1"=>15,"B1"=>20); 
      foreach($XlsColumnWidth as $Clm=>$Width) 
      { 
       //Set Columns Width 
       $this->XLSHandle->ActiveSheet->Range($Clm.":".$Clm)->ColumnWidth = $Width; 
      }  
     } 
     if($XlsColumnNames != null) 
     { 
      //$XlsColumnNames = array("FirstColumnName"=>1, "SecondColumnName"=>2); 
      foreach($XlsColumnNames as $ClmName=>$ClmNumber) 
      { 
       // Cells(Row,Column) 
       $this->XLSHandle->ActiveSheet->Cells(1,$ClmNumber)->Value = $ClmName; 
       $this->XLSHandle->ActiveSheet->Cells(1,$ClmNumber)->Font->Bold = True; 
       $this->XLSHandle->ActiveSheet->Cells(1,$ClmNumber)->Interior->ColorIndex = "15"; 
      } 
     } 
    } 
    //56 is for xls 8 
    public function SaveCreatedFile($FileName, $FileFormat = 56) 
    { 
     $this->xlBook->SaveAs($FileName, $FileFormat); 
    } 

    public function MakeFileVisible() 
    { 
     //Hide MS Excel application window`enter code here` 
     $this->XLSHandle->Visible = 1; 
    } 
}//end of EXCEL class 
1

有一個偉大的article解釋如何通過PHP代碼讀取/寫入excel文件,他們已被推薦使用MS-Excel Stream Handler PHP類,這是該類頂級類庫之一:)