10

Update

Tested using PHPSpreadsheet I have this code below that I have tried. It seems to work on MSOffice Excel if I use xls when I write the file. Note not working with Libre Office does not auto resize row need it to work with libre office as well.

$spreadsheet->getActiveSheet()->getRowDimension(1)->setRowHeight(-1);

foreach($spreadsheet->getActiveSheet()->getRowDimensions() as $rowID) { 
    $rowID->setRowHeight(-1); 
}

New Controller

<?php

require(APPPATH . 'vendor/autoload.php');

use PhpOffice\PhpSpreadsheet\Spreadsheet;

class Events extends MX_Controller {

    public function test() {
        $spreadsheet = new Spreadsheet();

        $spreadsheet->getDefaultStyle()->getFont()->setName('Arial');
        $spreadsheet->getDefaultStyle()->getFont()->setSize(24);

        foreach(range('A','B') as $columnID) {
            $spreadsheet->getActiveSheet()->getColumnDimension($columnID)->setAutoSize(true);
        }

        $spreadsheet->getActiveSheet()->getRowDimension(1)->setRowHeight(-1);

        foreach($spreadsheet->getActiveSheet()->getRowDimensions() as $rowID) { 
            $rowID->setRowHeight(-1); 
        }

        $spreadsheet->setActiveSheetIndex(0)
                ->setCellValue("A1",'Firstname')
                ->setCellValue("B1",'Lastname');

        $spreadsheet->getActiveSheet()->setTitle('Users Information');

        $spreadsheet->setActiveSheetIndex(0);

        /* Here there will be some code where you create $spreadsheet */

        // redirect output to client browser
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="myfile.xls"');
        header('Cache-Control: max-age=0');

        $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xls');
        $writer->save('php://output');
                
        exit;

    }
}

Original Question

I am new to phpexcel When I download my file the cells are overlapping each other.

As you can see in image the are all bunched up when have large font size.

enter image description here

Question How can I make sure the cells are not over lapping each other. I tried file in libreoffice & msoffice excel and same issue.

I have tried still no change

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

foreach(range(1, 4) as $rowID) {
  $objPHPExcel->getActiveSheet()->getRowDimension($rowID)->setRowHeight(-1);
}

Controller

<?php

class Events extends MX_Controller {

    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');

        $objPHPExcel->getActiveSheet()->getPageSetup()->setOrientation(PHPExcel_Worksheet_PageSetup::ORIENTATION_LANDSCAPE);
        $objPHPExcel->getActiveSheet()->getPageSetup()->setPaperSize(PHPExcel_Worksheet_PageSetup::PAPERSIZE_A4);

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

        $headerstyle = array(
            'font'  => array(
            'size'  => 25,
            'name'  => 'Candara'
            )
        );

        $objPHPExcel->getActiveSheet()->getStyle('A1:D1')->applyFromArray($headerstyle);


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

        $excelrow = 2;

        foreach ($excelresults as $excelresult => $excelvalue) {

            $columnstyle = array(
                'font'  => array(
                'size'  =>25,
                'name'  => 'Candara'
                )
            );

            $objPHPExcel->getActiveSheet()->getStyle('A' . $excelrow . ':D' . $excelrow)->applyFromArray($columnstyle);


            $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++;
        }

        ///exit();

        $filename = 'Bowling-Events-For-' . date('Y') . '.xlsx';

        $objPHPExcel->getProperties()->setTitle("Riwaka Bowling Club Events");

        header("Content-Type: application/vnd.ms-excel; charset=utf-8"); # Important 
        header("Content-Type: application/force-download");
        header("Content-Type: application/octet-stream");
        header("Content-Type: application/download");

        header("Content-Disposition: attachment; filename=".$filename."");
        header("Content-Transfer-Encoding: binary");

        header("Pragma: no-cache");
        
        header("Expires: 0");
        
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
        header("Cache-Control: private",false);

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

        exit();
    }
}
Community
  • 1
  • 1
  • 1
    Place the `setAutoSize()` call *after* setting all the values, i.e. after `foreach ($excelresults as $excelresult => $excelvalue) {` that `foreach`. Also, `setAutoSize()` is for *width*, not *height*, so you might still not get expected results. Use this on the rows too: `$objPHPExcell->getActiveSheet()->getRowDimension(1)->setRowHeight(-1);` – ishegg Oct 01 '17 at 04:37
  • @ishegg Thank you will try your way soon just way from main PC at the moment. –  Oct 01 '17 at 04:39
  • @ishegg tried it your way not working –  Oct 01 '17 at 05:04
  • Did you try it like [this](https://3v4l.org/3upYJ)? – ishegg Oct 01 '17 at 05:10
  • Yes still not working properly. –  Oct 01 '17 at 05:14
  • Are you using LibreOffice? – ishegg Oct 01 '17 at 05:15
  • @ishegg Yes correct –  Oct 01 '17 at 05:17
  • I see, there seems to be a bug in it. Try setting autowrapping: `$objPHPExcel->getActiveSheet()->getStyle("A1")->getAlignment()->setWrapText(true);` – ishegg Oct 01 '17 at 05:18
  • Stiil not working –  Oct 01 '17 at 05:35
  • I really do get fed up with this bug in Libre/OpenOffice https://bugs.documentfoundation.org/show_bug.cgi?id=62268 – Mark Baker Oct 02 '17 at 13:22
  • @MarkBaker I tried it in MSoffice same problem –  Oct 02 '17 at 18:59
  • So show the code where you're setting the row heights – Mark Baker Oct 02 '17 at 19:00
  • @MarkBaker I did it like this https://3v4l.org/3upYJ from ishegg comment –  Oct 02 '17 at 19:06
  • Does it work correctly when the files generated by /Examples/01simple.php are loaded in MS Excel, because rows 8, 10 and 12 are autoheight in that example, and I've just tested it without any problems – Mark Baker Oct 02 '17 at 19:24
  • @MarkBaker I will re try again with the code later I am just way from main PC atm –  Oct 02 '17 at 19:30
  • @MarkBaker still no change I updated question I get my data from database –  Oct 02 '17 at 21:00
  • So /Examples/01simple.php doesn't work; what version of MS Excel are you using? – Mark Baker Oct 02 '17 at 21:05
  • Try `$excel->getActiveSheet()->getDefaultRowDimension()->setRowHeight(-1)` – José A. Zapata Oct 03 '17 at 16:00
  • @JoséA.Zapata all ready have the -1 does not work –  Oct 03 '17 at 19:26
  • @MarkBaker Hi, I have stitched to using the PHPOffice/spreadsheet is there a auto height for that one? –  Oct 04 '17 at 02:49
  • @MarkBaker I have updated the question the `->setRowHeight(-1)` it does not make it auto row height it is the cause of it over lapping –  Oct 04 '17 at 04:02
  • PHPSpreadsheet has exactly the same method fr setting the row height, and if PHPExcel doesn't work correctly, then neither will PHPSpreadsheet..... what version of MS Excel are you using? If it doen't work for that version, then it needs fixing, but it certainly work for 95, 2013 and 2016 – Mark Baker Oct 04 '17 at 07:01
  • All current version both MSoffice and librieoffice –  Oct 04 '17 at 07:03
  • @MarkBaker I have been reading through your user guide I have found out a couple of things now. If I use xls when I write file the code works fine on msoffice I have code here https://pastebin.com/raw/9xGW6vNj but not sure how to tweak it for libre office now latest version –  Oct 04 '17 at 07:44

1 Answers1

8

please check the code below, is working for me:

$excel->getActiveSheet()->getRowDimension(1)->setRowHeight(-1);

To change height of all rows to auto you can do:

foreach($xls->getActiveSheet()->getRowDimensions() as $rowID) { 
    $rowID->setRowHeight(-1); 
}
funder7
  • 1,622
  • 17
  • 30
Ravi Chauhan
  • 1,409
  • 13
  • 26
  • I have been reading through user guide I have found out a couple of things now. If I use xls when I write the code works fine on msoffice I have code here https://pastebin.com/raw/9xGW6vNj but not sure how to tweak it for libre office now latest version –  Oct 04 '17 at 07:52
  • @Ravi Chauhan Maybe you can help me. Look at this : https://stackoverflow.com/questions/56469303/how-do-i-make-automatic-height-row-based-on-content-in-the-maatwebsite-version-3 – moses toh Jun 06 '19 at 02:45