I'm looking to import .csv data into my database.
The problem I'm having is the following are "sets" of data ( data
, valor
, descrição
) per line, and the screen is being displayed one below the other.
MyPHP:
<formaction="" 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>";
}
?>