检查列为空,或在使用phpexcel上传数据时在其中添加数据

时间:2019-07-06 14:13:14

标签: php mysql

我当前正在使用phpexcel库读取要由用户上传的excel文件。如果列是否上传(是否添加了数据),我需要在上传后获得结果,方法是使用get insert id include(date)行并显示添加了哪些列, 我在下面使用此代码(注释将详细说明一些步骤):

  if (isset($_POST['upload'])){
           //Include PHPExcel_IOFactory.....
           //upload key ...    
       $name       = $_FILES['myup']['name'];  
       $temp_name  = $_FILES['myup']['tmp_name'];
       if(isset($name)){
           if(!empty($name)){      
               $location = 'upload/';      
               if(move_uploaded_file($temp_name, $location.$name)){

                   $sheetIdQuery = "select max(sheet_id) as maxSheetId from `excel_table`";
                   $sheetQueryResult = mysqli_query($conn, $sheetIdQuery);
                   $sheetArray = mysqli_fetch_array($sheetQueryResult);

                   $maxSheetId = $sheetArray['maxSheetId']+1;
                   $objPHPExcel = PHPExcel_IOFactory::load($location.$name);
                   $ids=array();

               foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {
                $worksheetTitle     = $worksheet->getTitle();
                $highestRow         = $worksheet->getHighestRow(); // e.g. 10
                $highestColumn      = $worksheet->getHighestDataColumn(); // e.g 'F'
                $highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
                   $nrColumns = PHPExcel_Cell::columnIndexFromString($highestColumn);




                   //  import data to DataBase.......        
                       for ($row = 2; $row <= $highestRow; ++ $row) {
                           $val=array();
                           $ids=$row
                       for ($col = 0; $col < $highestColumnIndex; ++ $col) {
                           $cell = $worksheet->getCellByColumnAndRow($col, $row);
                           $val[] = $cell->getcalculatedValue();

                               }

                           $sql = "INSERT INTO `excel_table`(`sheet_id`, `Stores_Coding`,`Store`,`Sectors`,`Zone`,`Type`,`Received_date`)
                                   VALUES ('".$maxSheetId."', '".$val[0]."','".$val[1]."','".$val[2]."','".$val[3]."')";
                           $resultsd2 = mysqli_query($conn, $sql);



                       if(!$resultsd2){
                           echo $sql;
                           die(mysqli_error($conn));

                               }
                           }
                       if($resultsd2) {
                           echo '<div class="alert alert-success">
                                   Successfully Upload ExcelSheet....
                               </div>';
                               }else {

                                   echo '<div class="alert alert-danger">
                                           Error on Date Format and Faild To Upload ExcelSheet....
                                       </div>';
                               }
                           //read excel .....
                               echo "<br><div style='font-size:17px'>The worksheet ".$worksheetTitle." has ";
                               echo $nrColumns . ' columns (A-' . $highestColumn . ') ';
                               echo ' and ' . $highestRow . ' row.';
                               echo "</div><br/>";
                               echo '<br><div style="font-size:16px">Data: <table class="table
   table-bordered"><thead><tr>';
                               echo "
                                   <th style='display:none'>Id</th>
                                   <th style='display:none'>sheet Id</th>
                                   <th>Stores Coding</th>
                                   <th>Store</th>

                               ";
                               echo '</tr>';     
                               echo '</thead>';    
                               echo '<tbody>';
                               $selQ= "select * from excel_table where sheet_id='$maxSheetId'";
                               $selQResult = mysqli_query($conn, $selQ);
                           while($row= mysqli_fetch_array($selQResult)){
                                   echo "<tr>";
                                   echo '<td style="display:none">' . $row['Id'] . '</td>';
                                   echo '<td style="display:none">' . $row['sheet_id'] . '</td>';
                                   echo '<td>'.$row["Stores_Coding"].'</td>';
                                   echo '<td>'.$row["Store"].'</td>';

                               }

                                       echo '</tbody>';
                                       echo '</table>';
                                       echo '</div>';

                               }

                           }

0 个答案:

没有答案