删除PHPexcel中的空第一行

问题描述 投票:0回答:1

我正在使用phpexcel将excel文件上传到网站上的数据库。但是第一行被跳过并输入了我的数据。结果如下图所示。

我需要帮助..

我该如何解决?

enter image description here

我有以下代码。

<?
    include_once('./_common.php'); //database option 
    include 'Classes/PHPExcel.php'; 
    include 'Classes/PHPExcel/IOFactory.php'; //load phpexcel

    $up_file = "aaa.xlsx";
    try {
        $objReader = PHPExcel_IOFactory::createReaderForFile($up_file);

        //set the read only
        $objReader->setReadDataOnly(true);

        //read a excel file
        $objExcel = $objReader->load($up_file);

        // select first cell
        $objExcel->setActiveSheetIndex(0);

        $objWorksheet = $objExcel->getActiveSheet();

        $rowIterator = $objWorksheet->getRowIterator();


        foreach ($rowIterator as $row) {
                   $cellIterator = $row->getCellIterator();
                   $cellIterator->setIterateOnlyExistingCells(false);
        }

        $maxRow = $objWorksheet->getHighestRow();
        $maxCell = $objWorksheet->getHighestColumn();
         for ($i = 0 ; $i <= $maxRow; $i++) {

                 $acell = $objWorksheet->getCell('A' . $i)->getValue(); // A
                 $bcell = $objWorksheet->getCell('B' . $i)->getValue(); // B

                 $acell  = addslashes($acell);
                 $bcell  = addslashes($bcell);

              //$sql = "insert into echo_test (a,b,c,d,e) values ('$a','$b','$c','$d','$e')";
                $sql = "insert into echo_excel set 
                bcat = '$acell',
                scat = '$bcell',

              ";
              sql_query($sql);
          }
       echo $maxRow . " Data inserting finished !";



    } catch (exception $e) {
        echo 'error!';
    }

    ?>
php sql database phpmyadmin phpexcel
1个回答
0
投票

在尝试向表中插入数据之前,只需测试$bcat变量是否为空。

$acell = $objWorksheet->getCell('A' . $i)->getValue(); // A
$bcell = $objWorksheet->getCell('B' . $i)->getValue(); // B

if (empty($bcell)) {continue;}  // no value for bcat so go to the next row

$acell  = addslashes($acell);
$bcell  = addslashes($bcell);
© www.soinside.com 2019 - 2024. All rights reserved.