Kamis, 08 September 2011

Export Excel With Codeigniter

Bismillah ^_^


Asumsi Temen-temen sudah memilik codeigniter di htdocs
 1. ketikan syntax ini dan di simpan di dalam folde controller dengan nama baca_excel.php 
  <?php
 
class Baca_excel extends Controller {
 
    public function __construct() {
        parent::Controller() ;
        $this->load->helper('form') ;
    }
 
    function index() {     
        $this->load->view('upload_form', array('error' => ' ' ));
    }
 
    function do_upload()
    {
         
        $file =  $_SERVER['DOCUMENT_ROOT']."/ci-excel/public/upload/" ;
 
        $config['upload_path'] = $file;
        $config['allowed_types'] = 'xls';
        $config['max_size']    = '1000';
         
        $this->load->library('upload', $config);
         
        if ( ! $this->upload->do_upload())
        {
            $error = array('error' => $this->upload->display_errors());
             
            echo $error ;
        }     
        else
        {
            $upload_data = $this->upload->data();
             
            echo "<h3>Your file was successfully uploaded!</h3>" ;
 
            echo "<ul>" ;
            foreach($upload_data as $item => $value) {
                echo '<li>',$item,' : ',$value,'</li>';
            }
            echo "</ul>" ;
             
            echo "<p>== Data read file excel ==================================== //</p>" ;
 
            // Load the spreadsheet reader library
            $this->load->library('excel_reader');
 
            // Set output Encoding.
            $this->excel_reader->setOutputEncoding('CP1251');
 
            $file =  $file.$upload_data['file_name'] ;
 
            $this->excel_reader->read($file);
 
            error_reporting(E_ALL ^ E_NOTICE);
 
            // Sheet 1
            $data = $this->excel_reader->sheets[0] ;
 
            for ($i = 1; $i <= $data['numRows']; $i++) {
                for ($j = 1; $j <= $data['numCols']; $j++) {
                    echo "\"".$data['cells'][$i][$j]."\",";
                }
                echo "<br />";
            }             
 
        }
    }
 
 
}
 
?>

2. kita buat view dengan nama upload_form.php


<html>
<head>
<title>Upload Form</title>
</head>
<body>

<?php echo $error;?>

<?php echo form_open_multipart('welcome/do_upload');?>

<input type="file" name="userfile" size="20" />

<br /><br />

<input type="submit" value="upload" />

</form>

</body>
</html>

3. kemudian pada config->database.php, setting seperti ini

$db['default']['hostname'] = "localhost";
$db['default']['username'] = "root";
$db['default']['password'] = "";
$db['default']['database'] = "excel";
$db['default']['dbdriver'] = "mysql";
$db['default']['dbprefix'] = "";
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = "";
$db['default']['char_set'] = "utf8";
$db['default']['dbcollat'] = "utf8_general_ci";

buat database dengan nama excel kemudian buat tabel dengan nama mhs

Tidak ada komentar:

Posting Komentar