0
I’m looking to import data .csv into my database.
The problem I’m having is this, it’s "sets" of data (data
, valor
, descrição
), by line, being that on the screen is being displayed one below the other.
My PHP:
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="arquivo">
<input type="submit" name="pega" value="pega">
</form>
<?php
ini_set('display_errors', true);
error_reporting(E_ALL);
if (isset($_POST['pega'])) {
include_once("PHPExcel/Classes/PHPExcel.php");
$uploadDir = "uploadFile/";
$uploadfile = $uploadDir . $_FILES['arquivo']['name'];
if(move_uploaded_file($_FILES['arquivo']['tmp_name'], $uploadfile)) {
echo "Arquivo pego com sucesso";
echo "<br>";
}else{
echo "Não foi possível pegar arquivo";
echo "<br>";
}
$objReader = PHPExcel_IOFactory::createReader('Excel2007');
$objPHPExcel = $objReader->load($uploadfile);
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'CSV');
$csvFileName = str_replace('.xlsx', '.csv', $uploadfile);
$objWriter->save($csvFileName);
if (($handle = fopen($csvFileName, "r")) !== false) {
while (($data = fgetcsv($handle, 1000, ",")) !== false) {
$num = count($data);
for ($c = 0; $c < $num; $c++) {
echo $data[$c] . "<br />\n";
}
}
fclose($handle);
}
$objReader = new PHPExcel_Reader_Excel5();
$objReader->setReadDataOnly(true);
$objPHPExcel = $objReader->load("uploadfile");
$colunas = $objPHPExcel->setActiveSheetIndex(0)->getHighestColumn();
$totalColunas = PHPExcel_Cell::columnByIndexFromString($colunas);
$totalLinhas = $objPHPExcel->setActiveSheetIndex(0)->getHighetRows();
echo "<table border='1'>";
for ($linha=1; $linha <= $totalLinhas; $linha++) {
echo "<tr>";
for ($coluna=0; $coluna <= $totalColunas; $coluna++) {
if ($linha == 1) {
echo "<th>".utf8_decode($objPHPExcel->getActiveSheet()->getCellColumnAndRow($coluna,$linha)->getValue());
}else{
echo "<th>".utf8_decode($objPHPExcel->getActiveSheet()->getCellColumnAndRow($coluna,$linha)->getValue());
}
}
echo "</tr>";
}
echo "</table>";
}
?>
What problem are you having? To read or to print the file?
– Rodrigo Rigotti
Print the data on the screen... Because with them "organized", I want to play them in a comic book.
– GustavoSevero
It is a csv or an excel spreadsheet?
– rray
It’s an Excel spreadsheet, but I’m already converting . xlsx to . csv, in php.
– GustavoSevero
Idea @rodrigoRigotti and @rray?
– GustavoSevero
I think the part about csv is right, you noticed who a
<br>
there ->echo $data[$c] . "<br />\n";
, plays the<br>
after the is, see if it is the expected result.– rray
Perfect @rray, helped. Thanks.
– GustavoSevero