Skip to content

Instantly share code, notes, and snippets.

@puncoz
Created December 7, 2016 06:50
Show Gist options
  • Save puncoz/be433b02238625807c1088ac739b4d71 to your computer and use it in GitHub Desktop.
Save puncoz/be433b02238625807c1088ac739b4d71 to your computer and use it in GitHub Desktop.
<?php
( ! defined('BASEPATH')) OR exit('No direct script access allowed');
class Excel extends CI_Controller {
public function index() {
$this->excel_to_mysql();
}
public function excel_to_mysql() {
include './libraries/PHPExcel/Classes/PHPExcel.php';
$inputFileName = './file/file.xlsx';
// if file upload
// $inputFileName = $_FILES['FILE']['tmp_name'];
$inputFileType = PHPExcel_IOFactory::identify($inputFileName);
$objPHPExcelReader = PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcelReader->setReadDataOnly(true);
$objPHPExcel = $objPHPExcelReader->load($inputFileName);
$objWorksheet = $objPHPExcel->getActiveSheet();
$highestRow = $objWorksheet->getHighestRow();
$highestColumn = $objWorksheet->getHighestColumn();
$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
$rows = array();
$db_col = array('A', 'Region', 'Sub_Recepients', 'Working_Districts', 'VDC', 'Program_Component', 'SC_Focal_Person', 'Contact_Person_in_SR', 'Position', 'Contact_num', 'Mobile', 'e-mail');
for ($row = 1; $row <= $highestRow; ++$row) {
for ($col = 0; $col <= $highestColumnIndex-1; ++$col) {
$row_count = $row;
if($row != 1) {
$row_count--;
}
$rows[$row_count][$db_col[$col]] = $objWorksheet->getCellByColumnAndRow($col, $row)->getValue();
}
}
$this->db->insert_batch('test_table', $rows);
echo '<pre>';
print_r($rows);
}
public function excel_to_html() {
include './libraries/PHPExcel/Classes/PHPExcel.php';
$inputFileName = './file/file.xlsx';
$outputFileType = 'HTML';
$outputFileName = 'php://output';
$inputFileType = PHPExcel_IOFactory::identify($inputFileName);
$objPHPExcelReader = PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcel = $objPHPExcelReader->load($inputFileName);
$objPHPExcelWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, $outputFileType);
$objPHPExcel = $objPHPExcelWriter->save($outputFileName);
}
}
/* End of file Excel.php */
/* Location: ./application/controllers/Excel.php */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment