Generate Excel Report from MySQL Database using Codeigniter
and without using any library
Here we will see how to generate excel report from MySQL database using Codeigniter. Codeigniter is a of the popular PHP framework and it’s easy to work with Codeigniter for now you can export excel file without using any library.
Follow below steps.
- 1. Create a database table in MySQL database.
- 2. And insert the data in MySQL database .
- 3. Then Create a Controller.
- 4. And next create a query fecha data in MySQL database
- 5. And Create a view file.
- 6. And Finaly generate Excel Report
Requirements
To complete this tutorial you will need a fully generate Excel Repor:
- MySQL database
- Codeigniter
1. Create a database table in MySQL database.
CREATE TABLE `students` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(100) NOT NULL,
`father_name` varchar(100) NOT NULL,
`roll_number` double NOT NULL,
`date` varchar(10) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
2. Insert the data in students table .
insert into students(id,name,father_name,roll_number,date) values(1, 'Alamgir','Belel hossen','35000','02-04-2019');
insert into students(id,name,father_name,roll_number,date) values(2, 'Abdullah','Belel hossen','37030','03-04-2019');
insert into students(id,name,father_name,roll_number,date) values(3, 'Yanni','Belel hossen','48700','04-04-2019');
insert into students(id,name,father_name,roll_number,date) values(4, 'Zaman','Belel hossen','17505','05-04-2019');
insert into students(id,name,father_name,roll_number,date) values(5, 'Alpona','Belel hossen','22080','05-04-2019');
insert into students(id,name,father_name,roll_number,date) values(6, 'Labonno','Belel hossen','34040','05-04-2019');
insert into students(id,name,father_name,roll_number,date) values(7, 'Shams','Belel hossen','2500','06-04-2019');
insert into students(id,name,father_name,roll_number,date) values(8, 'Hasan','Belel hossen','55894','07-04-2019');
3. Create a controller .
So create a new file inside application/controller/Excel.php
Folder directory application/controller/Excel.php
Add below function index() and function excel_create() to the existing controller. Please modify the existing code from the controller.
defined('BASEPATH') OR exit('No direct script access allowed');
class Excel extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('Common_model');
}
/*************excel_create this function use**************/
public function index(){
$this->data['results'] = $this->Common_model->get_info('students');
$this->load->view('excel/index', $this->data);
}
public function excel_create()
{
$this->data['results'] = $this->Common_model->get_info('students');
$this->load->view('excel/excel_create', $this->data, true);
}
}
4. Create a model
Then we fetch the students information from MySQL database and write row information to the Excel array.
if (!defined('BASEPATH')) exit('No direct script access allowed');
class Common_model extends CI_Model {
public function __construct() {
parent::__construct();
}
public function get_info($table) {
$query = $this->db->get($table);
$ret = $query->result_array();
return $ret;
}
}
5.Create a view file
Then we fetch the students information list for index page.
Folder directory application/views/excel/index.phpNow we will add just one hyperlink on which user will click to generate exce and action excel_create() function.
Excel Report Export
//Now we will add just one hyperlink on which user will click to generate exce
Sl
Name
Father Name
Roll Number
Date
$row): ?>
=$row["id"]
=$row["name"] ?>
=$row["father_name"] ?>
=$row["roll_number"] ?>
=$row["date"] ?>
6 . Then we fetch the students information list for excel_create page.
Folder directory application/views/excel/excel_create.phpFinally we write the data to excel file.
Sl
Name
Father Name
Roll Number
Date
$row): ?>
=$row["id"]
=$row["name"] ?>
=$row["father_name"] ?>
=$row["roll_number"] ?>
=$row["date"] ?>
7 .Finally Output
CakePHP Tutorials
C programming
BlockChain & Ethereum
Smart Contract in Solidity
4k background with Blockchain
PHP Based Frameworks
Web Design Tutorials
Entertainment