Export data to excel in php codeigniter

In this blog, we will learn how to Export data into Excel in CodeIgniter. Export data functionality makes your web application user-friendly and helps the user to maintain list data. There are various file formats are available to export data and download it as a file. Microsoft Excel is a widely used spreadsheet format that organizes and maintains data.

Step 1: Download PHP_XLSXWriter Library.

This library is designed to be lightweight, and have minimal memory usage.

Step 2: Copy paste above downloaded file xlsxwriter.class.php inside application/models folder

Step 3: Now create one view file like swiftcode_report_view.php inside application/views folder and write below code

<form method="post" action="https://app.shinerweb.com/index.php/bankcode/download_swift_code">
<input type="submit" value="Download SWIFT Code Report">

Step 4 : Now create one file bankcode.php inside your application/controllers folder and write below code

class bankcode extends CI_Controller { 
	public function __construct() {
			$this->load->helper(array('form', 'url'));
    function swift_code_report()
	function download_swift_code()

You can access your view page with this link: https://app.shinerweb.com/index.php/bankcode/swift_code_report

Step 5: Once your view and controller code completed after that you need to create one more file bankcode_model.php inside your application/models folder

error_reporting(E_ALL ^ E_DEPRECATED);	
class bankcode_model extends CI_Model 
function download_swift_code()
$query=$this->db->query("SELECT BANK, CITY, SWIFT_CODE
FROM BANK_SWIFT_CODE"); //take your table here

$cnt = 2;
$rowcount = $query->num_rows();
	if($rowcount != 0)

	ini_set('display_errors', 0);
	ini_set('log_errors', 1);
	error_reporting(E_ALL & ~E_NOTICE);

	$filename = "SWIFT_Code_Report.xlsx";
	header('Content-disposition: attachment; filename="'.XLSXWriter::sanitize_filename($filename).'"');
	header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
	header('Content-Transfer-Encoding: binary');
	header('Cache-Control: must-revalidate');
	header('Pragma: public');

	foreach ($query->result_array() as $row)
	$arr[$i][0]=$row['BANK'];//your table column fields

	$header = array(
	'SWIFT Code'=>'string'
        //for date and time 'DD/MM/YYYY hh:mm'
        //for number value ''integer''

	$writer = new XLSXWriter();
	$writer->setAuthor('Divyesh Patel'); 
	$writer->writeSheetHeader('Sheet1', $header, $col_options = array('fill'=>'#699E96','font-style'=>'bold','color'=>'#ffffff', 'border'=>'left,right,top,bottom', 'border-style'=>'thin', 'height'=>15,'wrap_text'=>true,'valign'=>'center','halign'=>'center') );
	$border = array( 'border'=>'left,right,top,bottom');
	$border_style = array( 'border-style'=>'thin');
	foreach($arr as $row)
	$writer->writeSheetRow('Sheet1', $row, $border, $border_style);
	echo "<script>alert('No record(s) found.') </script>";  
	// Redircet to Home Page		
	echo "<script language=\"javascript\">window.location = '".	site_url('/bankcode/swift_code_report')."'  </script>";
//end function

For more details please click here

Related Posts

Divyesh Patel

I'm Divyesh Patel, Web & App developer. I want to make things that make a difference. With every line of code, i strive to make the web a beautiful place.

Leave a Reply