0

I am exporting a list from my database using PHPExcel.

But the cells are not evenly spaced as shown in image.

Question: How can I make sure that each cell is automatically evenly spaced in PHPExcel

I have tried the answers here but not work properly

PHPExcel How to apply styles and set cell width and cell height to cell generated dynamically

enter image description here

Controller Code

<?php

class Events extends MX_Controller {

    private $error = array();

    public function __construct() {
        parent::__construct();
    }

    public function generate_excel() {

        $query = $this->db->get('event');
        $excelresults = $query->result_array();

        require (APPPATH . 'third_party/PHPExcel-1.8/Classes/PHPExcel.php');
        require (APPPATH . 'third_party/PHPExcel-1.8/Classes/PHPExcel/Writer/Excel2007.php');

        $objPHPExcel = new PHPExcel();

        $objPHPExcel->getProperties()->setCreator("");
        $objPHPExcel->getProperties()->setLastModifiedBy("");

        $objPHPExcel->getProperties()->setSubject("");
        $objPHPExcel->getProperties()->setCreator("");
        $objPHPExcel->getProperties()->setDescription("");

        $objPHPExcel->setActiveSheetIndex(0);

        $objPHPExcel->getActiveSheet()->SetCellValue("A1", 'Event');
        $objPHPExcel->getActiveSheet()->SetCellValue("B1", 'Event Title');
        $objPHPExcel->getActiveSheet()->SetCellValue("C1", 'Event Date');
        $objPHPExcel->getActiveSheet()->SetCellValue("D1", 'Event Start Time');

        $excelrow = 2;

        foreach ($excelresults as $excelresult => $excelvalue) {
            $objPHPExcel->getActiveSheet()->SetCellValue("A" . $excelrow, $excelvalue['event']);
            $objPHPExcel->getActiveSheet()->SetCellValue("B" . $excelrow, $excelvalue['event_title']);
            $objPHPExcel->getActiveSheet()->SetCellValue("C" . $excelrow, $excelvalue['event_date']);
            $objPHPExcel->getActiveSheet()->SetCellValue("D" . $excelrow, $excelvalue['event_start_time']);

            $excelrow++;
        }

        $filename = 'Bowling-Events-For ' . date('Y') . '.xlsx';
        $objPHPExcel->getProperties()->setTitle("Riwaka Bowling Club Events");

        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        header('Content-Disposition: attachment;filename="' .$filename. '"');
        header('Cache-Control: max-age=0');

        $Writer = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
        $Writer->save('php://output');

        exit;
    }
}

1 Answers1

1

Solved found solution

Did some more google searching from answer here PHPExcel auto size column width

foreach (range('A', $objPHPExcel->getActiveSheet()->getHighestDataColumn()) as $col) {
    $objPHPExcel->getActiveSheet()
            ->getColumnDimension($col)
            ->setAutoSize(true);
    } 

enter image description here