Skip to main content
adicionou 981 caracteres ao conteúdo
Fonte Link
GustavoSevero
  • 2,6mil
  • 3
  • 36
  • 78
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><br>";
}else{
    echo "Não foi possível pegar arquivo";
    echo "<br><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]." ";
        }
        echo "<br />\n";
    }
    fclose($handle);
}
 
}

Consegui separar os dados, mas não consigo jogar pro banco, estou usando isso:

        $pdo = conectar();
        $insereDados=$pdo->prepare("INSERT INTO dadosImportados (data, tipo, valor, descricao) VALUES (?,?,?,?)");
        $insereDados->bindValue(0, $data[0]); 
        $insereDados->bindValue(1, $data[1]); 
        $insereDados->bindValue(2, $data[2]); 
        $insereDados->bindValue(3, $data[3]); 

        $insereDados->execute();

Mas essa mensagem aparece na tela: "Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY093]: Invalid parameter number: Columns/Parameters are 1-based' in /Applications/MAMP/htdocs/sistemas/scripts_da_web/php/importa/importaArquivo.php:68 Stack trace: #0 /Applications/MAMP/htdocs/sistemas/scripts_da_web/php/importa/importaArquivo.php(68): PDOStatement->bindValue(0, '10-01-15') #1 {main} thrown in /Applications/MAMP/htdocs/sistemas/scripts_da_web/php/importa/importaArquivo.php on line 68"

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><br>";
}else{
    echo "Não foi possível pegar arquivo";
    echo "<br><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]." ";
        }
        echo "<br />\n";
    }
    fclose($handle);
}
 
}
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><br>";
}else{
    echo "Não foi possível pegar arquivo";
    echo "<br><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]." ";
        }
        echo "<br />\n";
    }
    fclose($handle);
}
}

Consegui separar os dados, mas não consigo jogar pro banco, estou usando isso:

        $pdo = conectar();
        $insereDados=$pdo->prepare("INSERT INTO dadosImportados (data, tipo, valor, descricao) VALUES (?,?,?,?)");
        $insereDados->bindValue(0, $data[0]); 
        $insereDados->bindValue(1, $data[1]); 
        $insereDados->bindValue(2, $data[2]); 
        $insereDados->bindValue(3, $data[3]); 

        $insereDados->execute();

Mas essa mensagem aparece na tela: "Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY093]: Invalid parameter number: Columns/Parameters are 1-based' in /Applications/MAMP/htdocs/sistemas/scripts_da_web/php/importa/importaArquivo.php:68 Stack trace: #0 /Applications/MAMP/htdocs/sistemas/scripts_da_web/php/importa/importaArquivo.php(68): PDOStatement->bindValue(0, '10-01-15') #1 {main} thrown in /Applications/MAMP/htdocs/sistemas/scripts_da_web/php/importa/importaArquivo.php on line 68"

removeu 833 caracteres do conteúdo
Fonte Link
GustavoSevero
  • 2,6mil
  • 3
  • 36
  • 78
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><br>";
}else{
    echo "Não foi possível pegar arquivo";
    echo "<br><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]." ";
        }
        echo "<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>";

}
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><br>";
}else{
    echo "Não foi possível pegar arquivo";
    echo "<br><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]." ";
        }
        echo "<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>";

}
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><br>";
}else{
    echo "Não foi possível pegar arquivo";
    echo "<br><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]." ";
        }
        echo "<br />\n";
    }
    fclose($handle);
}

}
título editado
Link
Daniel Omine
  • 20,1mil
  • 1
  • 35
  • 67

Formatando dados para .csv Formatar data e valor monetário para depois jogarsalvar no banco de dados

adicionou 10 caracteres ao conteúdo
Fonte Link
Chun
  • 7,5mil
  • 4
  • 32
  • 57
Carregando
Código
Fonte Link
GustavoSevero
  • 2,6mil
  • 3
  • 36
  • 78
Carregando
Fonte Link
GustavoSevero
  • 2,6mil
  • 3
  • 36
  • 78
Carregando