[SOLVED] New lines in a cell

Topics: Developer Forum
Jul 28, 2008 at 8:19 PM
Hi,

I'm trying to create a new line in a cell
eg: $objPHPExcel->getActiveSheet()->setCellValue('A1', "first line\nsecond line");

but \n is not interpreted
How could I do this please ?
I tried \n \r\n but nothing :(
Developer
Jul 29, 2008 at 1:15 AM
Your methods looks correct, but Microsoft Office Excel 2007 only seems to (visually) render the new line whenever "Wrap Text" is set to true for the cell. Therefore you need to do like this:

$objPHPExcel->getActiveSheet()->setCellValue('A1', "first line\nsecond line");
$objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setWrapText(true);

In Microsoft Office Excel 2007 you insert a new line via ALT+Enter and, as can be tried, when you do that, the software will automatically set "Wrap Text" to true.
Jul 29, 2008 at 10:46 AM
Thx very much koyama, it's working :D
Apr 1, 2009 at 7:29 AM
Edited Apr 1, 2009 at 7:31 AM
yeah, setting Wrap property to true helped me too.

I tried "\r\n", "\n", tried chr() instead of 'values', tried just everything I can imagine. in excel it represented like a box at the place, where "\n" should be.
then I searched the discussions and found an answer and it works. thank you! :)
Oct 28, 2013 at 7:45 AM
hi, I tried "\r" or "\n" but i doesn't work. here my script. help me please .. :D
//create excel
        $filepath = "resource/doc/excel/fileexcel.xls";
        //---
        $objReader = PHPExcel_IOFactory::createReader('Excel5');
        $this->phpexcel = $objReader->load($filepath);
        $this->phpexcel->setActiveSheetIndex(0);
        $objWorksheet = $this->phpexcel->getActiveSheet();  
        
        $nama_bulan = $this->datetimemanipulation->get_month_name($filter['k_filter_month']);
        $objWorksheet->setCellValue('A5', 'PERIODE : \nBULAN '.  strtoupper($nama_bulan).' '.$filter['k_filter_years']);
        $objWorksheet->getStyle('A5')->getAlignment()->setWrapText(true);

       $file_name = 'TESTING';
        if($filter['k_filter_month']!='' && $filter['k_filter_years']!=''){
            $file_name .= $filter['k_filter_month'].'_'.$filter['k_filter_years'];
        }
        //--
        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        header('Content-Disposition: attachment;filename=' . strtoupper($file_name) . '.xlsx');
        header('Cache-Control: max-age=0');
        // output
        $obj_writer = PHPExcel_IOFactory::createWriter($this->phpexcel, 'Excel2007');
        $obj_writer->save('php://output');