Monday, March 4, 2013

Upload And Read File Excel With PHPExcel and CI

Step by step :

1. Create file upload.php in View
script :

<form method="post" enctype="multipart/form-data" action="read_excel/read_file">
Silakan Pilih File Excel: <input name="userfile" type="file">
<input name="upload" type="submit" value="Import">
</form>

2. Create Class Action in Controller with name read_excel.php
script :

<?php

class Read_excel extends CI_Controller {
  function Read_excel(){
    parent::__construct();
    $this->load->helper('form'); // untuk menangani proses form
  }

  function index() {
    $this->load->view('upload');
  }

  function read_file(){
      /** Include path **/
    set_include_path(get_include_path() . PATH_SEPARATOR . '../../../Classes/');

    /** PHPExcel_IOFactory */
    require_once 'Classes/PHPExcel/IOFactory.php';


    $inputFileType = 'Excel5';
    /**  Create a new Reader of the type defined in $inputFileType  **/
    $objReader = PHPExcel_IOFactory::createReader($inputFileType);
   
    /**  Load $inputFileName to a PHPExcel Object  **/
    $objPHPExcel = $objReader->load($_FILES['userfile']['tmp_name']);
   
    foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {
    $worksheetTitle     = $worksheet->getTitle();
    $highestRow         = $worksheet->getHighestRow(); // e.g. 10
    $highestColumn      = $worksheet->getHighestColumn(); // e.g 'F'
    $highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
    $nrColumns = ord($highestColumn) - 64;
   
    echo "<br>The worksheet ".$worksheetTitle." has ";
    echo $nrColumns . ' columns (A-' . $highestColumn . ') ';
    echo ' and ' . $highestRow . ' row.';
   
    echo '<br>Data: <table border="1"><tr>';
   
    for ($row = 1; $row <= $highestRow; ++ $row) {
        echo '<tr>';
       
        for ($col = 0; $col < $highestColumnIndex; ++ $col) {
            $cell = $worksheet->getCellByColumnAndRow($col, $row);
            $val = $cell->getValue();
           
            echo '<td>' . $val . '</td>';
        }
        echo '</tr>';
    }
    echo '</table>';

  }
 
}
}
?>

3. Please run !!

Copilot - Membuat Aplikasi Camera Detection menggunakan flutter

  Langkah-langkah Persiapkan Lingkungan Flutter : Pastikan Flutter sudah terinstal di sistem Anda. Jika belum, Anda bisa mengikuti panduan i...