用PHP读取Excel文件

我正在尝试读取一个Excel文件(Office 2003)。 有一个Excel文件需要上传并parsing其内容。

通过谷歌,我只能find这些相关(和不足的主题)的答案:生成Excel文件,读取Excel XML文件,读取Excel CSV文件,或不完整的废弃项目。 我拥有的Office 2003,所以如果我需要从那里的任何文件,他们是可用的。 它安装在我的盒子上,但不是也不能安装在我的共享主机上。

编辑:到目前为止,所有的答案指向PHP-ExcelReader和/或这个额外的文章如何使用它。

我使用PHP-ExcelReader读取xls文件,效果很好。

据我所知,你有2个select:

  1. Spreadsheet_Excel_Reader ,它知道Office 2003的二进制格式
  2. PHPExcel ,它知道Office 2003以及Excel 2007(XML)。

PHPExcel使用Spreadsheet_Excel_Reader作为Office 2003格式。

更新:我曾经读过一些Excel文件,但是我使用了Office 2003 XML格式来读取它们,并告诉正在使用该应用程序的人员仅保存和上传该types的Excel文件。

这取决于你想如何使用Excel文件中的数据。 如果你想把它导入到mysql,你可以简单的把它保存为一个CSV格式的文件,然后用fgetcsvparsing它。

尝试这个…

我用下面的代码来读取“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

有一个伟大的文章,以解释如何通过PHP代码读取/写入excel文件,他们已被推荐使用MS-Excelstream处理程序PHP类,这是顶级类库之一:)

 // 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