PHPExcel – Library PHP untuk baca excel

Berikut adalah library untuk membaca file excel di PHP, penggunaan PHPExcel mudah digunakan dan cukup lengkap. silahkan download link di .

https://www.4shared.com/zip/HHdZECm1ca/PHPExcel-develop.html

Adapun cara pemanggilan library PHPExcel di script PHP

require_once 'PHPExcel-develop/Classes/PHPExcel.php';
require_once 'PHPExcel-develop/Classes/PHPExcel/Reader/Excel2007.php';
require_once 'PHPExcel-develop/Classes/PHPExcel/IOFactory.php';

contoh sederhana pemakaian library sebagai berikut.

// 
 $objPHPExcel = PHPExcel_IOFactory::load("uploads/" . $file_import);
 
 foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {

$worksheetTitle = $worksheet->getTitle();
 $highestRow = $worksheet->getHighestRow(); // e.g. 10
 $highestColumn = 'D'; //$worksheet->getHighestColumn(); // e.g 'F'
 $highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
 
 for ($row = 2; $row <= $highestRow; ++ $row) {
 
 $dataRow = array();
 for ($col = 0; $col < $highestColumnIndex; ++ $col) {
 $cell = $worksheet->getCellByColumnAndRow($col, $row);
 $val = $cell->getValue();
 
 $dataRow[$col] = $val;
 }

if(count($dataRow)>0) {

if(trim(strip_tags(addslashes($dataRow[1]))) != '') {

$data = array(
 "col_1" => trim(strip_tags(addslashes($dataRow[1]))),
 "col_2" => trim(strip_tags(addslashes($dataRow[2]))),
 "col_3" => trim(strip_tags(addslashes($dataRow[3]))),
 "col_4" => trim(strip_tags(addslashes($dataRow[4]))),
 );

$arr_col = "";
 $arr_val = "";
 foreach($data as $key => $val) {
 $arr_col[] = $key;
 $arr_val[] = "'" . $val . "'";
 }
 $str_col = implode(", ", $arr_col);
 $str_val = implode(", ", $arr_val);

// 
 $sql = "insert into nama_table ( " . $str_col . " )
 values ( " . $str_val . " );";

mysql_query($sql);
 }

}
 } 
 }

Selebihnya anda bisa kembangkan sendiri.

wassalam.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s