1
I have a file in xlsx format with two spreadsheets and I need to read the data in PHP to create an import script in the database.
I’m using the Phpspreadsheet library to read the files. I can do when you only have one spreadsheet in the document, but when there are others it doesn’t work.
Test document.xlsx
Code to submit file: index php.
<!DOCTYPE html>
<html>
<head>
<title>Importacao</title>
</head>
<body>
<h1>Upload do arquivo</h1>
<form method="POST" action="processa.php" enctype="multipart/form-data">
<label>Arquivo</label>
<input type="file" name="arquivo"><br><br>
<input type="submit" value="enviar">
</form>
</body>
</html>
Code to read data and display in an HTML table: parses.php
<?php
require 'vendor/autoload.php';
use PhpOffice\PhpSpreadsheet\Spreadsheet; //classe responsável pela manipulação da planilha
function readData($arquivo){
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader("Xlsx");
$spreadsheet = $reader->load($arquivo);
$sheet = $spreadsheet->getActiveSheet();
//Coluna - Retira o título(2)
echo '<table border="1" cellpadding="8" style="margin-left:100px;">';
foreach ($sheet->getRowIterator(2) as $row) {
$cellInterator = $row->getCellIterator();
$cellInterator->setIterateOnlyExistingCells(false);
echo '<tr>';
//Linha
foreach ($cellInterator as $cell) {
if(!is_null($cell)){
$value = $cell->getCalculatedValue();
echo "<td> $value </td>";
}
}
echo '</tr>';
}
echo "</table>";
}
$dados = $_FILES['arquivo'];
var_dump($dados);
$route = $_FILES['arquivo']['tmp_name'];
readData($route);
//$route = 'spreadsheet1.xlsx';
if(!empty($route)){
}else{
echo "null";
}
?>
Recalling q the code works to read a file with only one spreadsheet. What I need is for him to read the other spreadsheets present in the same file (below the editor Planilla1 and Planilla2
Very good guy!! It worked here. However a problem arose here when submitting my file, it seems that it is very big. This error appears: Allowed memory size of 268435456 bytes exhausted (tried to allocate 4096 bytes) in /var/www/dir/script/vendor/phpoffice/phpspreadsheet/src/Phpspreadsheet/Reader/Xlsx.php on line 999
– Flávio Filipe
That’s a problem(Docs) with the memory limitation that PHP imposes on scripts. Either you will have to go releasing memory after use, or increase the available memory for the script.
– fernandosavio