mk-j / PHP_XLSXWriter

Lightweight XLSX Excel Spreadsheet Writer in PHP
MIT License
1.84k stars 665 forks source link

wrap_text is not working #287

Closed shoroar closed 1 year ago

shoroar commented 4 years ago

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] );
}`
AzzaAzza69 commented 3 years ago

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']);
shoroar commented 3 years ago

Nothing changed. Same problem :(

Is my code correct?

Please drop an example of your working code.

AzzaAzza69 commented 3 years ago

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

screenshot

shoroar commented 3 years ago

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;

AzzaAzza69 commented 3 years ago

Here's my copy of the libary with my modifications - try that...

xlsx-writer.zip

shoroar commented 3 years ago

Hello,

Thank you very much. It's perfectly working now.

shoroar commented 3 years ago

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.

AzzaAzza69 commented 3 years ago

Try changing the {...} to [...]...I don't use php 7.4 yet so I can only offer this advice...

shoroar commented 3 years ago

Hello @AzzaAzza69

Did it already and by this, the error notice of curly braces disappear but the generated excel sheet is not open :(

AzzaAzza69 commented 3 years ago

Sorry, I don't use php 7.4 so I can't help :(