0

I have multiple address for a record and i want to add line break for each address with a cell named "Address". I tried adding "\r\n" AND PHP_EOL which didn't worked as desired.

Here is my code:

$indx = 1;
            $adr_data = "";
            foreach ($a_users_dtls as $ckt_dtls) {
                foreach ($ckt_dtls as $usr_dtls) {
                    $stat = '';
                    $header_row.= $indx++ . "\t" . $usr_dtls['name'] . "\t" . $usr_dtls['email_id'] ."\t".$usr_dtls['id'] ."\t" . $usr_dtls['cisdr'] . "\t" . $stat;
                    if ($showAddress1 == 1){
                        $address_list = explode("##,", $usr_dtls['address']); 
                        if(!empty($address_list)){
                            foreach($address_list as $key => $value){
                                $adr_data .=rtrim($value,',##')."\r\n";
                            }
                        }
                        $header_row .= " \t ".$adr_data;
                    }
                    if ($showDisplayCity == 1)
                        $header_row .= " \t ".$usr_dtls['display_city'];
                    $header_row .= " \n ";
                }
            }
            $filename = "user_list_" . date("Y_m_d") . "_" . time() . ".xls";
            header('Content-type: application/ms-excel');
            header('Content-Disposition: attachment; filename="' . $filename . '"');
            echo($header_row);
            exit;

When i try the above code i gets address in new rows of excel and not within the cell. I want to add line breaks within the cell named "Address" so that all address gets populated within a cell if there are multiple addresses.

Any help will be highly appreciated as i am new to excel generation via php.

Wolverine
  • 455
  • 3
  • 8
  • 26
  • you need to use cell **Text Wrapping** so it will wrap text instead of moving to next cell. – jagad89 Jul 06 '15 at 08:34
  • possible duplicate of http://stackoverflow.com/questions/7765652/line-break-within-data-for-excel-2003%E2%80%8C%E2%80%8B ? – Abdo Adel Jul 06 '15 at 08:36
  • Aside from the fact that this is a CSV file rather than an Excel file, you should be able to use "\n" inside a cell and "\r\n" as the actual end of CSV line, as long as the cell is quoted text (wrapped in `"`).... but why do so many developers insist on using bad homebrew code to create a csv when this would be easy if they only uses PHP's built-in [fputcsv()](http://php.net/manual/en/function.fputcsv.php) function – Mark Baker Jul 06 '15 at 08:50
  • @AbdoAdel sorry $lfcr = chr(10) . chr(13); adding this to each line didn't worked – Wolverine Jul 06 '15 at 08:50
  • @jagad89 how can i add text wrapping dynamically any examples?? – Wolverine Jul 06 '15 at 08:51
  • @MarkBaker hi thanks for your response but i am pulling data from database and doing a for loop to generate excel using php.... the excel is generating correctly but only new line within a cell is not working.... the above suggestions didn't worked for me.... – Wolverine Jul 06 '15 at 08:55
  • Show how you implemented my suggestion, because you're still showing code that doesn't quote string values in any way, or use fputcsv() – Mark Baker Jul 06 '15 at 09:12
  • @jagad89 cell wrapping can't be applied to a CSV file, only to a real BIFF or OfficeOpenXML format Excel file – Mark Baker Jul 06 '15 at 09:13
  • @MarkBaker In question he has used **.xls** format, So i thought he is writing to Excel sheet manually, So i thought it is possible. I also agree with your comment. – jagad89 Jul 06 '15 at 09:28
  • Like many people, he believes that a file with an xls extension is an xls file, even though it's a csv with tab separator that's he's actually writing.... he's not even sending the correct mime type as his content type... it should be `application/vnd.ms-excel` for an xls file – Mark Baker Jul 06 '15 at 09:29
  • @MarkBaker Yes Exactly. it is actually **.csv** file with **\t** delimiter. – jagad89 Jul 06 '15 at 09:33
  • @MarkBaker thanks for correcting me... i just started with excel i will try ur suggestion and let u know.... – Wolverine Jul 06 '15 at 10:07

0 Answers0