How to customize the excel data's with two sheets in one excel with php? How to customize the excel data's with two sheets in one excel with php? codeigniter codeigniter

How to customize the excel data's with two sheets in one excel with php?


Yes have to download PHPExcel library for codeigniter.

I have some sample code which help you to work on PHPExcel.

    function test_excel()        {            $this->load->library('excel');            $this->excel->setActiveSheetIndex(0);            $this->excel->getActiveSheet()->setTitle('test worksheet');            $this->excel->getActiveSheet()->setCellValue('A1', 'User id');            $this->excel->getActiveSheet()->setCellValue('B1', 'User name');            $this->excel->getActiveSheet()->setCellValue('C1', 'Email');            $this->excel->getActiveSheet()->setCellValue('D1', 'Status');            $this->excel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);            $this->excel->getActiveSheet()->getStyle('B1')->getFont()->setBold(true);            $this->excel->getActiveSheet()->getStyle('C1')->getFont()->setBold(true);            $this->excel->getActiveSheet()->getStyle('D1')->getFont()->setBold(true);            $query = $this->db->query("SELECT * FROM users");            $k=2;            foreach($query->result_array() as $row)            {                $this->excel->getActiveSheet()->setCellValue("A".$k, $row['USER_ID']);                $this->excel->getActiveSheet()->setCellValue("B".$k, $row['FIRST_NAME']);                $this->excel->getActiveSheet()->setCellValue("C".$k, $row['USER_EMAIL']);                $this->excel->getActiveSheet()->setCellValue("D".$k, $row['USER_TYPE_ID']);                $k++;            }            $filename='just_some_random_name.xls';             header('Content-Type: application/vnd.ms-excel');            header('Content-Disposition: attachment;filename="'.$filename.'"');             header('Cache-Control: max-age=0'); //no cache            $objWriter = PHPExcel_IOFactory::createWriter($this->excel, 'Excel5');              $objWriter->save('php://output');        }

Hope it will help you.


Hi @Jagan Akash check this,

public function test_excel()     {         $this->load->library('excel');        $this->excel->setActiveSheetIndex(0);        $this->excel->getActiveSheet()->setTitle('test worksheet');        $this->excel->getActiveSheet()->setCellValue('A1', 'User id');        $this->excel->getActiveSheet()->setCellValue('B1', 'User name');        $this->excel->getActiveSheet()->setCellValue('C1', 'Email');        $this->excel->getActiveSheet()->setCellValue('D1', 'Status');        $this->excel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);        $this->excel->getActiveSheet()->getStyle('B1')->getFont()->setBold(true);        $this->excel->getActiveSheet()->getStyle('C1')->getFont()->setBold(true);        $this->excel->getActiveSheet()->getStyle('D1')->getFont()->setBold(true);        $query = $this->db->query("SELECT * FROM user limit 2");        $k=2;        foreach($query->result_array() as $row)        {            $this->excel->getActiveSheet()->setCellValue("A".$k, $row['user_id']);            $this->excel->getActiveSheet()->setCellValue("B".$k, $row['first_name']);            $this->excel->getActiveSheet()->setCellValue("C".$k, $row['last_name']);            $this->excel->getActiveSheet()->setCellValue("D".$k, $row['user_email']);            $k++;        }        $init_cnt = $this->excel->getSheetCount();        $this->excel->createSheet($init_cnt);        $this->excel->setActiveSheetIndex($init_cnt);        $this->excel->getActiveSheet()->setTitle('test 1');        $this->excel->getActiveSheet()->setCellValue('A1', '1');        $this->excel->getActiveSheet()->setCellValue('B1', '2');        $this->excel->getActiveSheet()->setCellValue('C1', '3');        $this->excel->getActiveSheet()->setCellValue('D1', '4');        $this->excel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);        $this->excel->getActiveSheet()->getStyle('B1')->getFont()->setBold(true);        $this->excel->getActiveSheet()->getStyle('C1')->getFont()->setBold(true);        $this->excel->getActiveSheet()->getStyle('D1')->getFont()->setBold(true);        $query = $this->db->query("SELECT * FROM user limit 2,2");        $k=2;        foreach($query->result_array() as $row)        {            $this->excel->getActiveSheet()->setCellValue("A".$k, $row['user_id']);            $this->excel->getActiveSheet()->setCellValue("B".$k, $row['first_name']);            $this->excel->getActiveSheet()->setCellValue("C".$k, $row['last_name']);            $this->excel->getActiveSheet()->setCellValue("D".$k, $row['user_email']);            $k++;        }        $init_cnt = $this->excel->getSheetCount();        $this->excel->createSheet($init_cnt);        $this->excel->setActiveSheetIndex($init_cnt);        $this->excel->getActiveSheet()->setTitle('test 2');        $this->excel->getActiveSheet()->setCellValue('A1', '12');        $this->excel->getActiveSheet()->setCellValue('B1', '22');        $this->excel->getActiveSheet()->setCellValue('C1', '32');        $this->excel->getActiveSheet()->setCellValue('D1', '42');        $this->excel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);        $this->excel->getActiveSheet()->getStyle('B1')->getFont()->setBold(true);        $this->excel->getActiveSheet()->getStyle('C1')->getFont()->setBold(true);        $this->excel->getActiveSheet()->getStyle('D1')->getFont()->setBold(true);        $query = $this->db->query("SELECT * FROM user limit 4,2");        $k=2;        foreach($query->result_array() as $row)        {            $this->excel->getActiveSheet()->setCellValue("A".$k, $row['user_id']);            $this->excel->getActiveSheet()->setCellValue("B".$k, $row['first_name']);            $this->excel->getActiveSheet()->setCellValue("C".$k, $row['last_name']);            $this->excel->getActiveSheet()->setCellValue("D".$k, $row['user_email']);            $k++;        }        $this->excel->setActiveSheetIndex(0);        $filename='just_some_random_name.xls';        header('Content-Type: application/vnd.ms-excel');        header('Content-Disposition: attachment;filename="'.$filename.'"');        header('Cache-Control: max-age=0');        $objWriter = PHPExcel_IOFactory::createWriter($this->excel, 'Excel5');          $objWriter->save('php://output');    }

This will give answer to all question.