Reputation: 108
I have array data set in column row and export excel but if long text added then line broken. Can you please suggest how to long text wrap?
$heading = false;
if(!empty($records)) {
foreach($records as $row) {
if(!$heading) {
// display field/column names as a first row
echo implode("\t", array_keys($row)) . "\n";
$heading = true;
}
echo implode("\t", array_values($row)) . "\n";
}
}
Upvotes: 0
Views: 1786
Reputation: 21619
Instead of:
echo implode("\t", array_keys($row)) . "\n";
use Chr to return ASCII Code 13:
echo implode("\t", array_keys($row)) . chr(13);
or use PHP_EOL:
echo implode("\t", array_keys($row)) . PHP_EOL;
Line breaks will only show in Excel for cells that have "Wor Wrap" enabled.
In your output Excel file:
highlight the data that was output by PHP
right-click on the highlighted cell(s)
choose Format Cells
go to Alignment tab
check box Word Wrap.
If that fixes the issue then your data is (correctly) breaking the line, but it's just Excel's word-wrap default that's preventing the line breaks. You can set it programmatically but it would need a different approach in writing to the file. An easier solution is setup the formatting (including Word Wrap) in the file prior to writing to it, like a template.
Upvotes: 1