tutorial simplexlsx phpspreadsheet leer guardar excelreader ejemplos desde descargar datos archivo php import-from-excel

simplexlsx - phpspreadsheet tutorial



Lectura de un archivo de Excel en PHP (6)

Estoy tratando de leer un archivo de Excel (Office 2003). Hay un archivo de Excel que debe cargarse y analizar su contenido.

A través de Google, solo puedo encontrar respuestas a estos temas relacionados (e insuficientes): generar archivos de Excel, leer archivos XML de Excel, leer archivos CSV de Excel o proyectos abandonados incompletos. Soy el propietario de Office 2003, así que si necesito algún archivo de allí, están disponibles. Está instalado en mi caja, pero no está ni puede instalarse en mi host compartido.

Editar: hasta ahora, todas las respuestas apuntan a PHP-ExcelReader y / o este artículo adicional sobre cómo usarlo.


Depende de cómo quiera usar los datos en el archivo de Excel. Si desea importarlo en mysql, simplemente puede guardarlo como un archivo con formato CSV y luego usar fgetcsv para analizarlo.


Hay un excelente article para explicar cómo leer / escribir archivos de Excel a través del código php. Se les ha recomendado utilizar la clase MS-Excel Stream Handler PHP, que es una de las mejores bibliotecas de clase para eso :)


Por lo que sé, tienes 2 opciones:

  1. PHP-ExcelReader , que conoce el formato binario de Office 2003
  2. PHPExcel , que conoce tanto Office 2003 como Excel 2007 (XML).

PHPExcel usa Spreadsheet_Excel_Reader para el formato de Office 2003.

Actualización: una vez tuve que leer algunos archivos de Excel, pero utilicé el formato XML de Office 2003 para leerlos y les dije a las personas que usaban la aplicación que guardasen y subieran solo ese tipo de archivo de Excel.


Prueba esto...

He usado el siguiente código para leer "xls y 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>'';

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



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