Welcome Guest, Not a member yet? Register   Sign In
how to update xlsx file data into DB using PHPExcel
#1

(This post was last modified: 06-25-2018, 11:51 PM by kvanaraj.)

I want to update xlsx cell data file into DB using PHPExcel .My cell value starting from Row 12. My Excel sheet printscreen attached. 
My controller file like 
$markid = $this->input->post('markid');
  echo $markid;         
  $asses = $this->input->post('sel_ass1');
  echo $asses;

      $csvMimes = array('application/vnd.ms-excel','text/plain','text/csv','text/tsv');
      if(!empty($_FILES['file']['name']) && in_array($_FILES['file']['type'],$csvMimes))
       {
          if(is_uploaded_file($_FILES['file']['tmp_name']))
            {
              
              //open uploaded csv file with read only mode
              $csvFile = fopen($_FILES['file']['tmp_name'], 'r');   
              // skip first line
              // if your csv file have no heading, just comment the next line
              fgetcsv($csvFile);            
              //parse data from csv file line by line
              while(($line = fgetcsv($csvFile)) !== FALSE)
              {
              //check whether member already exists in database with same email
              $result = $this->db->get_where("pre_marks", array('regno'=>$line[0],"markid"=>$line[1]))->result();
        //echo $result;
        if(count($result) > 0)
        {
         $this->db->update("pre_marks", array("ass1"=>$line[2],"ass2"=>$line[2],"ass3"=>$line[2]),array("regno"=>$line[0]));
        }
          fclose($csvFile);       
         
        $this->session->set_flashdata('success', 'updated successfully');
            redirect(site_url('assessment'));

I want to update each value into the corresponding regno key. kindly help me to solve my problem
. my exported file type is xlsx (2007).

Attached Files Thumbnail(s)
   
Reply
#2

(1) "not working" is not a helpful problem description. At a minimum, indicate what you expect to be happening, and what the problem or error message is.

(2) Use code tags to make long code fragments easier to read ... https://forum.codeigniter.com/misc.php?a...help&hid=7
Reply
#3

Hi

Here is my piece of code for uploading data into a table from xlsx file.
Few simple things for this:
- i use the 1st line to store the column names
- for my uploads the 1st column must always be populated

I have set up namespaces for my CI3 so i can use "use" tags before the class declaration in my controller/model
PHP Code:
use PhpOffice\PhpSpreadsheet\IOFactory;
use 
PhpOffice\PhpSpreadsheet\Reader\Xlsx;
use 
PhpOffice\PhpSpreadsheet\Spreadsheet;
use 
PhpOffice\PhpSpreadsheet\Style\Alignment;
use 
PhpOffice\PhpSpreadsheet\Style\Border;
use 
PhpOffice\PhpSpreadsheet\Style\Color;
use 
PhpOffice\PhpSpreadsheet\Style\Fill


PHP Code:
        $file '/path/to/your/file.xlsx';

 
       // Instantiate new reader
 
       $reader = new Xlsx();

 
       // Set to read data only
 
       $reader->setReadDataOnly(true);

 
       // Get the active sheet (without calculating functions and without formatting)
 
       $asheet $reader->load($file)->getActiveSheet()->toArray(nullfalsefalsefalse);

 
       // Set up arrays for insert ID's and header
 
       $header    = [];

 
       // Counter used for rows in foreach loop
 
       $c         0;

 
       // Start DB transaction
 
       $this->db->trans_start();

 
       // Loop through the active sheet
 
       foreach ($asheet as $a)
 
       {
 
           // for some reason we get a lot of empty arrays so we should only consider ones that
 
           // contain actual data (1st cell can't be blank for these uploads).
 
           if (!empty($a[0]))
 
           {
 
               // The first row is always the column headers
 
               if ($c == 0)
 
               {
 
                   $header $a;
 
               }
 
               else
                
{
 
                   // combine header and row data to get associative array for db insert
 
                   $temp array_combine($header$a);

 
                   $this->db->insert($table$temp);

 
                    unset($temp);
 
               
                
}
 
               $c ++;
 
           }
 
       }

 
       $this->db->trans_complete(); 
Reply




Theme © iAndrew 2016 - Forum software by © MyBB