PHP_XLSXWriter
PHP_XLSXWriter copied to clipboard
wrap_text is not working
Hello,
Is there any problem with my code? I want to break in the cell by "\n" but when I use wrap_text opening the downloaded sheet it shows a warning/error "Excel found unreadable content in fileName.xlsx. Do you want to recover the contents of this workbook? if you trust the workbook, click Yes "
When I do not use the $row_options = ['wrap_text'=>true] in the writeSheetRow, the error/warning disappear and the file shows as expected but the file ignores the "\n" in a cell and put all the information in one line into the cell. I need to break down the information by "/n"
Here are my code blocks, Am I missing something?
`$sheet_name = 'Sheet1';
$writer = new XLSXWriter();
$writer->writeSheetHeader($sheet_name, $header, $col_options = ['widths'=>[10,15,15,30,15,30,30,30,10,20,30], 'font-style' => 'bold', 'fill'=>'#ccc', 'suppress_row'=>false] );
foreach ($excel_sheet_data as $row) {
$writer->writeSheetRow($sheet_name, $row, $row_options = ['wrap_text'=>true] );
}`
try this:
... add this function to the class
private function boolToStr($value){
return $value ? 'true' : 'false';
}
...find the line:
$style_indexes[$i]['wrap_text'] = (bool)$style['wrap_text'];
...change to:
$style_indexes[$i]['wrap_text'] = boolToStr((bool)$style['wrap_text']);
Nothing changed. Same problem :(
Is my code correct?
Please drop an example of your working code.
sorry, my mistake, ignore the changes I suggested, the boolToStr I used was to fix something else (see below):
$ht = isset($row_options['height']) ? floatval($row_options['height']) : 12.1;
$customHt = isset($row_options['height']) ? true : false;
$hidden = isset($row_options['hidden']) ? (bool)($row_options['hidden']) : false;
$collapsed = isset($row_options['collapsed']) ? (bool)($row_options['collapsed']) : false;
$sheet->file_writer->write('<row collapsed="'.$this->boolToStr($collapsed).
'" customHeight="'.$this->boolToStr($customHt).
'" hidden="'.$this->boolToStr($hidden).
'" ht="'.($ht).'" outlineLevel="0" r="' . ($sheet->row_count + 1) . '">');
Here is your test project with $header and $excel_sheet_data defined with dummy values. I could reproduce your problem only if I defined the heads as a simple array instead of key-value pairs (see commented out line).
$sheet_name = 'Sheet1';
$writer = new XLSXWriter();
// $header=['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k']; // using header like this, Excel will complain
$header=['a'=>'','b'=>'','c'=>'','d'=>'','e'=>'','f'=>'','g'=>'','h'=>'','i'=>'','j'=>'','k'=>'']; // this works fine...
$excel_sheet_data=[
['test1-a','test-b',"test\r\nof\r\nCRLF\r\nword-wrap",'test of auto word wrap without CRLF','test-e','test-f','test-g','test-h','test-i','test-j','test-k'],
];
$writer->writeSheetHeader($sheet_name, $header, $col_options = ['widths'=>[10,15,15,30,15,30,30,30,10,20,30], 'font-style' => 'bold', 'fill'=>'#ccc', 'suppress_row'=>false] );
foreach ($excel_sheet_data as $row) {
$writer->writeSheetRow($sheet_name, $row, $row_options = ['wrap_text'=>true] );
}
$writer->writeToFile('c:/temp/test.xlsx');
Hello,
Could you please check by the following codes? We still get the error notification in the file open and no data in the excel sheet
$filename = "sampleFilename.xlsx"; header('Content-disposition: attachment; filename="'.XLSXWriter::sanitize_filename($filename).'"'); header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); header('Content-Transfer-Encoding: binary'); header('Cache-Control: must-revalidate'); header('Pragma: public'); ob_clean(); flush();
$sheet_name = 'Sheet1'; $writer = new XLSXWriter(); $header=['a'=>'','b'=>'','c'=>'','d'=>'','e'=>'','f'=>'','g'=>'','h'=>'','i'=>'','j'=>'','k'=>'']; // this works fine... $excel_sheet_data=[ ['test1-a','test-b',"test\r\nof\r\nCRLF\r\nword-wrap",'test of auto word wrap without CRLF','test-e','test-f','test-g','test-h','test-i','test-j','test-k'], ]; $writer->writeSheetHeader($sheet_name, $header, $col_options = ['widths'=>[10,15,15,30,15,30,30,30,10,20,30], 'font-style' => 'bold', 'fill'=>'#ccc', 'suppress_row'=>false] ); foreach ($excel_sheet_data as $row) { $writer->writeSheetRow($sheet_name, $row, $row_options = ['wrap_text'=>true] ); }
$writer->writeToStdOut(); exit;
Here's my copy of the libary with my modifications - try that...
- fixed standalone time (without a date) ; before was returning 0!
- default font set to Calibri 11 (was Arial 10)
- added page properties
- changed margins to use page properties (was hardcoded)
- changed orientation to use page properties (was hardcoded)
- added header/footer functionality
- added default_row_height property (12.1 was hardcoded)
- added auto-scale to fit page width
- added row repeat (when printing pages, repeats headings across pages)
- fixed auto_filter was not writing out sheetname correctly
- changed order of files written to zip (fixes mime type detection problem)
- added improvement of speed when writing cell refs with columns <=ZZ
- fixed no longer strips lead/trailing spaces from sheetnames
- fixed stripping of unicode characters from sheetnames ; now tries to substitute
Hello,
Thank you very much. It's perfectly working now.
Hello @AzzaAzza69
I have to contact again as I see the xlsx-writer.zip you provided previously is working with PHP v7.3 but when I update my PHP version to 7.4, it is not working. There is a notice I find is, curly braces are deprecated and that is happening in line number 439 also the file is not opening.
Can you please help me with the issue? I will grateful to you to help me again.
Try changing the {...} to [...]...I don't use php 7.4 yet so I can only offer this advice...
Hello @AzzaAzza69
Did it already and by this, the error notice of curly braces disappear but the generated excel sheet is not open :(
Sorry, I don't use php 7.4 so I can't help :(