Full Stack LAMP - MEAN Developer, Python developer. Certified Azure Developer. Freelance programmer/consultant/trainer.

How to use PHPExcel with CodeIgniter?

PHPExcel is a pure PHP library for reading and writing spreadsheet files and CodeIgniter is one of the well-known PHP MVC Frameworks. In this tutorial, I am gonna show you how to Integrate the PHPEXcel library in CodeIgniter with simple steps.

Similar Posts
CodeIgniter 4 – Export data to excel in CodeIgniter using PhpSpreadsheet
How to generate excel from the array using PHPExcel
How to generate Excel using PhpSpreadsheet in CodeIgniter?

Step 1: Download and setup CodeIgniter.(download it here: https://ellislab.com/codeigniter)

Step 2: Download PHPExcel.(download it here: http://phpexcel.codeplex.com/)

Step 3: Unzip or extract the downloaded PHPExcel lib files and copy the class directory inside files to application/third-party the directory(folder).

step 4: Now create one file called EXcel.php in application/library folder [application/library/Excel.php]. Then include PHPExcel Class to it. see how I did it below

if (!defined('BASEPATH')) exit('No direct script access allowed');  

require_once APPPATH."/third_party/PHPExcel.php";
 
class Excel extends PHPExcel {
    public function __construct() {
        parent::__construct();
    }
}

That’s it now you can use PHPEXcel methods inside your CodeIgniter application.But you should load it before using it methods(Ex: $this->load->library(‘excel’);)

How to read an excel file

$file = './files/test.xlsx';
 
//load the excel library
$this->load->library('excel');
 
//read file from path
$objPHPExcel = PHPExcel_IOFactory::load($file);
 
//get only the Cell Collection
$cell_collection = $objPHPExcel->getActiveSheet()->getCellCollection();
 
//extract to a PHP readable array format
foreach ($cell_collection as $cell) {
    $column = $objPHPExcel->getActiveSheet()->getCell($cell)->getColumn();
    $row = $objPHPExcel->getActiveSheet()->getCell($cell)->getRow();
    $data_value = $objPHPExcel->getActiveSheet()->getCell($cell)->getValue();
 
    //The header will/should be in row 1 only. of course, this can be modified to suit your need.
    if ($row == 1) {
        $header[$row][$column] = $data_value;
    } else {
        $arr_data[$row][$column] = $data_value;
    }
}
 
//send the data in an array format
$data['header'] = $header;
$data['values'] = $arr_data;

How to Create an excel file on the file

//load our new PHPExcel library
$this->load->library('excel');
//activate worksheet number 1
$this->excel->setActiveSheetIndex(0);
//name the worksheet
$this->excel->getActiveSheet()->setTitle('test worksheet');
//set cell A1 content with some text
$this->excel->getActiveSheet()->setCellValue('A1', 'This is just some text value');
//change the font size
$this->excel->getActiveSheet()->getStyle('A1')->getFont()->setSize(20);
//make the font become bold
$this->excel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
//merge cell A1 until D1
$this->excel->getActiveSheet()->mergeCells('A1:D1');
//set aligment to center for that merged cell (A1 to D1)
$this->excel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
 
$filename='just_some_random_name.xls'; //save our workbook as this file name
header('Content-Type: application/vnd.ms-excel'); //mime type
header('Content-Disposition: attachment;filename="'.$filename.'"'); //tell browser what's the file name
header('Cache-Control: max-age=0'); //no cache
            
//save it to Excel5 format (excel 2003 .XLS file), change this to 'Excel2007' (and adjust the filename extension, also the header mime type)
//if you want to save it as .XLSX Excel 2007 format
$objWriter = PHPExcel_IOFactory::createWriter($this->excel, 'Excel5');  
//force user to download the Excel file without writing it to server's HD
$objWriter->save('php://output');

How to Generate Excel file with Database table data

		
		//load our new PHPExcel library
		$this->load->library('excel');
		//activate worksheet number 1
		$this->excel->setActiveSheetIndex(0);
		//name the worksheet
		$this->excel->getActiveSheet()->setTitle('Users list');

		// load database
		$this->load->database();

		// load model
		$this->load->model('userModel');

		// get all users in array formate
		$users = $this->userModel->get_users();

		// read data to active sheet
		$this->excel->getActiveSheet()->fromArray($users);

		$filename='just_some_random_name.xls'; //save our workbook as this file name

		header('Content-Type: application/vnd.ms-excel'); //mime type

		header('Content-Disposition: attachment;filename="'.$filename.'"'); //tell browser what's the file name

		header('Cache-Control: max-age=0'); //no cache
		            
		//save it to Excel5 format (excel 2003 .XLS file), change this to 'Excel2007' (and adjust the filename extension, also the header mime type)
		//if you want to save it as.XLSX Excel 2007 format

		$objWriter = PHPExcel_IOFactory::createWriter($this->excel, 'Excel5'); 

		//force user to download the Excel file without writing it to server's HD
		$objWriter->save('php://output');

That’s it.

  1. This example gives garbage characters in excel file…by google i found issue in this code is…
    it should

    $objWriter->save(‘test.xls’);
    instead of
    $objWriter->save(‘php://output’);

    it i also need some clean code..

  2. i’m having an error:

    A PHP Error was encountered

    Severity: Notice

    Message: Undefined property: CI_Loader::$excel

    Filename: pages/excel.php

    Line Number: 5

    helpp

  3. Hey its not working for me. But when I run your examples, they works perfectecly. I just copied your export code still getting garbage data when I open downloaded xls file. Thanks

    1. Serialize() converts an array into a normal string that you can pass in a URL,save as in database(wordpress uses) etc.
      Unserialize() is the opposite of serialize() – it takes a serialized string and converts it back to an array.

  4. Hello, Whenever I call this controller I get the following error:A PHP Error was encountered

    Severity: Error

    Message: Class ‘PHPExcel_IOFactory’ not found

    Filename: controllers/Result.php

    Line Number: 229
    line229: $objReader = PHPExcel_IOFactory::createReader(‘Excel2007’); please help me

      1. yes i can open base excel sheet .
        arjun..i need to retrieve data from mysql table to excel with all rows and colums as seen in tables..i can receive single row of data only …column name as we proceeded in phpexcel code.
        i need multiple rows with column names to assiciate excel sheet ..
        how it is.
        and i have to split rows as number sets (2000/20)…and once again i will to upload to mysql ..

        this may have any chance pls help me do it..
        thanks

          1. thanks arjun
            we need table colums in first row in excel
            we have 5000 questions and options and answers in the same table
            we have to spilit them all in sets cinsists of 500 per set and inner set should have 20 each..
            how to split them in excel ..
            can have a trick pls help me do

          2. yes thank for your reply arjun ..try to understand the picture .i m waitning for your response

          3. table having 13 columns and multiple rows per test ok ..
            we need to save 20 per excel sheet up to 500 *20 per test if test question having 5000 qn. for example ..
            i cannot create xls column with rows perfectly ..i m used array_merge its wrking on print_r() function only ..but its too print rows and columns as consequent index ..
            rows are printed as rows and columns are printed as rows when using array_merge function ..
            any possiblities pls i m in hurry ..

      1. I’ve tried every configuration I can find for the Excel2007 and not having any luck. Have you? I need a chart and charts don’t work in the Excel5…

  5. This works beautifully for Excel5 but when I try to update for Excel2007, I get nothing or a file that won’t open. I have tried every configuration of this (with and without ob_end_clean()). Is CodeIgniter doing something to get in the way?

    Excel5 works great:
    header(‘Content-type: application/vnd.ms-excel’);
    header(‘Content-Disposition: attachment;filename=”testfile.xls”‘);
    header(‘Cache-Control: max-age=0’);
    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, ‘Excel5’);
    $objWriter->save(‘php://output’);

    Excel2007 not so much:
    header(‘Content-type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet’);
    header(‘Content-Disposition: attachment;filename=”testfile.xlsx”‘);
    header(‘Cache-Control: max-age=0’);
    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, ‘Excel2007’);
    $objWriter->save(‘php://output’);

  6. Arjun, you’ve posted: “we are here to solve your problems.” Why then you do not reply requests for help? If you do not know how to solve it – write it in comment.

  7. Hi, Arjun
    thanks for this amazing tutorial, i follow all steps and its work fine on my local server but when i was upload this code live server i am getting (file not found) page. then i used ob_end_clean and some other code like change headers but nothing is going well, when i comment the last line of code $objWriter->save(‘php://output’); now its work fine and but blank excel-file download can you help me please. thanks in advance.

    $objWriter->save(‘php://output’);
    $objWriter->save(‘php://output’);

  8. ÐÏࡱá;þÿ
    þÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿþÿÿÿ

    þÿÿÿþÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿþÿà…ŸòùOh«‘+’³Ù0¸@Hh€˜

    ¤°äUntitled
    SpreadsheetUnknown CreatorUnknown
    Creator@€×¢wR€Ò@€×¢wR€Ò
    »

    ÌÑB°=¼%r8X”1ܐCalibri1¼Calibriàõÿ
    À
    àõÿ À
    àõÿ À
    àõÿ À
    àõÿ À
    àõÿ À
    àõÿ À
    àõÿ À
    àõÿ À
    àõÿ À
    àõÿ À
    àõÿ À
    àõÿ À
    àõÿ À
    àõÿ À
    à À
    à”È
    “€ÿ’â8ÿÿÿÿÿÿÿÿÿÿÿÿ€€€€€€€€€ÀÀÀ€€€™™ÿ™3fÿÿÌÌÿÿffÿ€€fÌÌÌÿ€ÿÿÿÿÿÿ€€€€€ÿÌÿÌÿÿÌÿÌÿÿ™™Ìÿÿ™ÌÌ™ÿÿÌ™3fÿ3ÌÌ™ÌÿÌÿ™ÿfff™–––3f3™f333™3™3f33™333…$“test
    worksheetÁÁgæ®üCThis
    is just some text value

    »

    ÌÑ*+‚€Áƒ„&ffffffæ?’ffffffæ?(è?)è?¡”dXX333333Ó?333333Ó?U}$
    }$
    }$
    }$
    ý

    >¶@d‹‹då

    ggÿÿÿÿÿ

    þÿÕÍÕœ.“—+,ù®0¼HP

    X`hp

    xŽä

    WorksheetFeuilles
    de calculRoot Entryÿÿÿÿÿÿÿÿ
    ÀF€×¢wR€Ò€×¢wR€Ò@SummaryInformation(ÿÿÿÿ
    ÀFèWorkbookÿÿÿÿÿÿÿÿÿÿÿÿ
    ÀF)DocumentSummaryInformation8ÿÿÿÿÿÿÿÿÿÿÿÿ
    ÀFìþÿÿÿþÿÿÿþÿÿÿýÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ

  9. Hello,
    Very help full post I have a query what if we want to download the same excel file in our directory , I am not asking about browser download option.

    Thanks.

  10. not open in linux

    function excelHarian(){
    // Load plugin PHPExcel nya
    include APPPATH.’third_party/PHPExcel/PHPExcel.php’;

    // Panggil class PHPExcel nya
    $excel = new PHPExcel();
    // Settingan awal fil excel
    $excel->getProperties()->setCreator(‘My Notes Code’)
    ->setLastModifiedBy(‘My Notes Code’)
    ->setTitle(“Laporan Pendapatan Harian”)
    ->setSubject(“Siswa”)
    ->setDescription(“Laporan Semua Data Siswa”)
    ->setKeywords(“Laporan Pendapatan Harian”);
    // Buat sebuah variabel untuk menampung pengaturan style dari header tabel
    $style_col = array(
    ‘font’ => array(‘bold’ => true), // Set font nya jadi bold
    ‘alignment’ => array(
    ‘horizontal’ => PHPExcel_Style_Alignment::HORIZONTAL_CENTER, // Set text jadi ditengah secara horizontal (center)
    ‘vertical’ => PHPExcel_Style_Alignment::VERTICAL_CENTER // Set text jadi di tengah secara vertical (middle)
    ),
    ‘borders’ => array(
    ‘top’ => array(‘style’ => PHPExcel_Style_Border::BORDER_THIN), // Set border top dengan garis tipis
    ‘right’ => array(‘style’ => PHPExcel_Style_Border::BORDER_THIN), // Set border right dengan garis tipis
    ‘bottom’ => array(‘style’ => PHPExcel_Style_Border::BORDER_THIN), // Set border bottom dengan garis tipis
    ‘left’ => array(‘style’ => PHPExcel_Style_Border::BORDER_THIN) // Set border left dengan garis tipis
    )
    );
    // Buat sebuah variabel untuk menampung pengaturan style dari isi tabel
    $style_row = array(
    ‘alignment’ => array(
    ‘vertical’ => PHPExcel_Style_Alignment::VERTICAL_CENTER // Set text jadi di tengah secara vertical (middle)
    ),
    ‘borders’ => array(
    ‘top’ => array(‘style’ => PHPExcel_Style_Border::BORDER_THIN), // Set border top dengan garis tipis
    ‘right’ => array(‘style’ => PHPExcel_Style_Border::BORDER_THIN), // Set border right dengan garis tipis
    ‘bottom’ => array(‘style’ => PHPExcel_Style_Border::BORDER_THIN), // Set border bottom dengan garis tipis
    ‘left’ => array(‘style’ => PHPExcel_Style_Border::BORDER_THIN) // Set border left dengan garis tipis
    )
    );
    $excel->setActiveSheetIndex(0)->setCellValue(‘A1’, “Laporan Pendapatan Harian”); // Set kolom A1 dengan tulisan “DATA SISWA”
    $excel->getActiveSheet()->mergeCells(‘A1:H1’); // Set Merge Cell pada kolom A1 sampai E1
    $excel->getActiveSheet()->getStyle(‘A1’)->getFont()->setBold(TRUE); // Set bold kolom A1
    $excel->getActiveSheet()->getStyle(‘A1’)->getFont()->setSize(15); // Set font size 15 untuk kolom A1
    $excel->getActiveSheet()->getStyle(‘A1’)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); // Set text center untuk kolom A1

    // Buat header tabel nya pada baris ke 3
    $excel->setActiveSheetIndex(0)->setCellValue(‘A3’, “NO”); // Set kolom A3 dengan tulisan
    $excel->setActiveSheetIndex(0)->setCellValue(‘B3’, “TANGGAL”); // Set kolom B3 dengan tulisan
    $excel->setActiveSheetIndex(0)->setCellValue(‘C3’, “MOTOR”); // Set kolom C3 dengan tulisan
    $excel->setActiveSheetIndex(0)->setCellValue(‘D3’, “MOBIL”); // Set kolom D3 dengan tulisan
    $excel->setActiveSheetIndex(0)->setCellValue(‘E3’, “BUS/TRUCK”); // Set kolom E3 dengan tulisan
    $excel->setActiveSheetIndex(0)->setCellValue(‘F3’, “BOX”); // Set kolom D3 dengan tulisan
    $excel->setActiveSheetIndex(0)->setCellValue(‘G3’, “TAKSI”); // Set kolom E3 dengan tulisan
    $excel->setActiveSheetIndex(0)->setCellValue(‘H3’, “LOKASI PARKIR”); // Set kolom D3 dengan tulisan
    $excel->setActiveSheetIndex(0)->setCellValue(‘I3’, “PENDAPATAN”); // Set kolom E3 dengan tulisan

    // Apply style header yang telah kita buat tadi ke masing-masing kolom header
    $excel->getActiveSheet()->getStyle(‘A3’)->applyFromArray($style_col);
    $excel->getActiveSheet()->getStyle(‘B3’)->applyFromArray($style_col);
    $excel->getActiveSheet()->getStyle(‘C3’)->applyFromArray($style_col);
    $excel->getActiveSheet()->getStyle(‘D3’)->applyFromArray($style_col);
    $excel->getActiveSheet()->getStyle(‘E3’)->applyFromArray($style_col);
    $excel->getActiveSheet()->getStyle(‘F3’)->applyFromArray($style_col);
    $excel->getActiveSheet()->getStyle(‘G3’)->applyFromArray($style_col);
    $excel->getActiveSheet()->getStyle(‘H3’)->applyFromArray($style_col);
    $excel->getActiveSheet()->getStyle(‘I3’)->applyFromArray($style_col);

    // Panggil function view yang ada di SiswaModel untuk menampilkan semua data siswanya
    $range_date = !empty($this->input->post(‘reservation’)) ? $this->input->post(‘reservation’) : ”;
    if (!empty($range_date)) {
    $start_date = substr($range_date, 0, -13);
    $end_date = substr($range_date, 13);
    $datetime1 = new DateTime($start_date);
    $datetime2 = new DateTime($end_date);
    $tanggal1 = $datetime1->format(‘Y-m-d’);
    $tanggal2 = $datetime2->format(‘Y-m-d’);

    $where = ‘date(tiket.waktuMasuktiket) BETWEEN”‘.$tanggal1.'” and “‘.$tanggal2.'”‘;
    }else {
    $where = null;
    }
    $siswa = $this->Mpendapatanharian->getAllTiket($where);

    $no = 1; // Untuk penomoran tabel, di awal set dengan 1
    $numrow = 4; // Set baris pertama untuk isi tabel adalah baris ke 4
    foreach($siswa as $data){ // Lakukan looping pada variabel siswa
    $excel->setActiveSheetIndex(0)->setCellValue(‘A’.$numrow, $no);
    $excel->setActiveSheetIndex(0)->setCellValue(‘B’.$numrow, $data[‘tanggal’]);
    $excel->setActiveSheetIndex(0)->setCellValue(‘C’.$numrow, $data[‘Motor’]);
    $excel->setActiveSheetIndex(0)->setCellValue(‘D’.$numrow, $data[‘Mobil’]);
    $excel->setActiveSheetIndex(0)->setCellValue(‘E’.$numrow, $data[‘BusTruck’]);
    $excel->setActiveSheetIndex(0)->setCellValue(‘F’.$numrow, $data[‘Box’]);
    $excel->setActiveSheetIndex(0)->setCellValue(‘G’.$numrow, $data[‘Taksi’]);
    $excel->setActiveSheetIndex(0)->setCellValue(‘H’.$numrow, $data[‘jumlahLokasi’]);
    $excel->setActiveSheetIndex(0)->setCellValue(‘I’.$numrow, $data[‘totalPendapatan’]);

    // Apply style row yang telah kita buat tadi ke masing-masing baris (isi tabel)
    $excel->getActiveSheet()->getStyle(‘A’.$numrow)->applyFromArray($style_row);
    $excel->getActiveSheet()->getStyle(‘B’.$numrow)->applyFromArray($style_row);
    $excel->getActiveSheet()->getStyle(‘C’.$numrow)->applyFromArray($style_row);
    $excel->getActiveSheet()->getStyle(‘D’.$numrow)->applyFromArray($style_row);
    $excel->getActiveSheet()->getStyle(‘E’.$numrow)->applyFromArray($style_row);
    $excel->getActiveSheet()->getStyle(‘F’.$numrow)->applyFromArray($style_row);
    $excel->getActiveSheet()->getStyle(‘G’.$numrow)->applyFromArray($style_row);
    $excel->getActiveSheet()->getStyle(‘H’.$numrow)->applyFromArray($style_row);
    $excel->getActiveSheet()->getStyle(‘I’.$numrow)->applyFromArray($style_row);

    $no++; // Tambah 1 setiap kali looping
    $numrow++; // Tambah 1 setiap kali looping
    }
    // Set width kolom
    $excel->getActiveSheet()->getColumnDimension(‘A’)->setWidth(5); // Set width kolom A
    $excel->getActiveSheet()->getColumnDimension(‘B’)->setWidth(15); // Set width kolom B
    $excel->getActiveSheet()->getColumnDimension(‘C’)->setWidth(10); // Set width kolom C
    $excel->getActiveSheet()->getColumnDimension(‘D’)->setWidth(10); // Set width kolom D
    $excel->getActiveSheet()->getColumnDimension(‘E’)->setWidth(15); // Set width kolom E
    $excel->getActiveSheet()->getColumnDimension(‘F’)->setWidth(10); // Set width kolom F
    $excel->getActiveSheet()->getColumnDimension(‘G’)->setWidth(10); // Set width kolom G
    $excel->getActiveSheet()->getColumnDimension(‘H’)->setWidth(20); // Set width kolom H
    $excel->getActiveSheet()->getColumnDimension(‘I’)->setWidth(20); // Set width kolom I

    // Set height semua kolom menjadi auto (mengikuti height isi dari kolommnya, jadi otomatis)
    $excel->getActiveSheet()->getDefaultRowDimension()->setRowHeight(-1);
    // Set orientasi kertas jadi LANDSCAPE
    $excel->getActiveSheet()->getPageSetup()->setOrientation(PHPExcel_Worksheet_PageSetup::ORIENTATION_LANDSCAPE);
    // Set judul file excel nya
    $excel->getActiveSheet(0)->setTitle(“Laporan Pendapatan Harian”);
    $excel->setActiveSheetIndex(0);
    // Proses file excel
    header(‘Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet’);
    header(‘Content-Disposition: attachment; filename=”Laporan Pendapatan Harian.xlsx”‘); // Set nama file excel nya
    header(‘Cache-Control: max-age=0’);
    $write = PHPExcel_IOFactory::createWriter($excel, ‘Excel2007’);
    ob_end_clean();
    $write->save(‘php://output’);
    exit;
    }

  11. how to fix eror

    [Wed Apr 21 09:45:42.797127 2021] [php:error] [pid 14384:tid 1808] [client ::1:55014] PHP Fatal error: Array and string offset access syntax with curly braces is no longer supported in C:\\xampp\\htdocs\\satudata\\application\\libraries\\PHPExcel\\Calculation.php on line 2763, referer: https://localhost/satudata/index.php/dda_dua/row_desainer/177

    if ((isset(self::$_comparisonOperators[$opCharacter])) && (strlen($formula) > $index) && (isset(self::$_comparisonOperators[$formula{$index+1}]))) {
    $opCharacter .= $formula{++$index};

    thankyou

Leave a Reply