109

Excelファイル(Office 2003)を読み込もうとしています。アップロードしてその内容を解析する必要があるExcelファイルがあります。

Google経由では、Excelファイルの生成、Excel XMLファイルの読み取り、Excel CSVファイルの読み取り、または不完全な放棄されたプロジェクトなど、これらの関連する(および不十分なトピック)に対する回答しか見つけることができません。私はOffice2003を所有しているので、そこからファイルが必要な場合は、それらを利用できます。ボックスにインストールされていますが、共有ホストにはインストールできません。

編集:これまでのところ、すべての回答はPHP-ExcelReaderおよび/またはその使用方法に関するこの追加記事を指しています。

4

8 に答える 8

62

私の知る限り、2つの選択肢があります。

  1. Spreadsheet_Excel_Reader、Office2003のバイナリ形式を認識している
  2. PHPExcel。Office2003とExcel2007(XML)の両方を認識しています。(リンクをたどると、彼らがこのライブラリをPHPSpreadSheetにアップグレードしたことがわかります)

PHPExcelは、Office2003形式にSpreadsheet_Excel_Readerを使用します。

更新:以前はいくつかのExcelファイルを読み取る必要がありましたが、それらを読み取るためにOffice 2003 XML形式を使用し、アプリケーションを使用しているユーザーにその種類のExcelファイルのみを保存およびアップロードするように指示しました。

于 2009-02-19T01:57:19.920 に答える
56

PHP-ExcelReaderを使用して xls ファイルを読み取ると、うまく機能します。

于 2009-02-19T03:07:02.593 に答える
20

それは、Excel ファイル内のデータをどのように使用するかによって異なります。これを mysql にインポートする場合は、単純に CSV 形式のファイルとして保存し、fgetcsvを使用して解析することができます。

于 2013-04-21T21:09:46.020 に答える
4
// 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
于 2015-09-30T23:40:01.387 に答える
4

これを試して...

次のコードを使用して「xls and 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

于 2015-07-04T11:42:00.893 に答える