简体   繁体   中英

Parsing Excel data from specific row and columns to MySQL using PHP

currently im doing my homework project to parse specific data from excel to MySQL using PHP. The project is about uploading an excel file and then the excel data supposed to be in MySQL after excel file uploaded.

The problem that im facing is, my homework project requirement is only parse specific row and column amongst the data exists in the excel file. I have already try using codes that i found, but it doesn't work. It parse everything in the excel files to MySQL table. Not the specific Row and Columns data that i expected.

I'm still looking for a solution for this. Spent almost 2 weeks for this but still i'm facing a dead end. My deadline project in 1 week. So, i would be really thankful if someone could help me and get me a solution of this.

I have tried on this PHP script. But, all the data in excel were parsed to MySQL table and the data messed the row and column in MySQL table. I want only the specific part (row and column) of excel files to be inserted to MySQL table that i've crated. i provided screenshot and the part that i want to be parsed to MySQL table highlighted with red color.

if(!empty($_FILES['excelfile']['name'])){
  // Get File extension eg. 'xlsx' to check file is excel sheet
  $pathinfo = pathinfo($_FILES['excelfile']['name']);

  // check file has extension xlsx, xls and also check
  // file is not empty
  if (($pathinfo['extension'] == 'xlsx' || $pathinfo['extension'] == 'xls')
      && $_FILES['excelfile']['size'] > 0 ){

          $file = $_FILES['excelfile']['tmp_name'];
          $reader = ReaderFactory::create(Type::XLSX);

          $reader->open($file);
          $count = 0;

          foreach ($reader->getSheetIterator() as $sheet){

              foreach($sheet->getRowIterator () as $row)
              {
                  if ($count > 0){
                      $name = $row[2];
                      $job_schedule = $row[3];
                      $overtime = $row[4];
                      $notes = $row[5];
                      $start_working = $row[6];    
                      $finished_working = $row[7];

                      $qry = "INSERT INTO `timesheet` (`name`,`job_schedule`, `overtime`,`notes`, `start_working`,`finished_working`) VALUES ('$name','$job_schedule','$overtime','$notes','$start_working','$finished_working')";
                      $res = mysqli_query($con,$qry);
                  }
                  $count++;
              }

          }

          if($res){
              echo "Success";
          }
          else{
              echo "failed";
          }
          $reader->close();
      }
      else{
          echo "Excel format is not supported";
      }

```[Here is the SCREENSHOT LINK]


[1]: https://i.stack.imgur.com/0kIxo.png

You can follow this link:

Exporting data from php to excel

I recommend use PHPExcel php class, just plug and play, I've done it myself. It's works better than trying to figure everthing by yourself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM