Excel file created from box/spout library is not editable in microsoft excel

649 views Asked by At

I just recently implemented box/spout library replacing the phpspreadsheet library in favour of memory efficiency.

I am also able to produce the excel file using it.

But after opening the excel file using ms office 2019, I cannot edit the cells at all, it just seems all shown read-only.

But if I use phpspreadsheet to generate the same thing, I can edit the file.

Do any of you folks know anything that I am missing out here.

Following is my implementation of box/spout

use Box\Spout\Common\Type;
use Box\Spout\Writer\Common\Creator\Style\StyleBuilder;
use Box\Spout\Writer\Common\Creator\WriterEntityFactory;
use PhpOffice\PhpSpreadsheet\Shared\Date;

function exprot_table($xmlObjectArray, $rows)
{
    error_reporting(E_ALL);
    $head = (array) $rows[0];
    $headArray = array_keys($head);

    $writer = WriterEntityFactory::createWriter(Type::XLSX);
    $writer->openToBrowser("demoexcel.xlsx");

    $rowFromValues = WriterEntityFactory::createRowFromArray($headArray);
    $writer->addRow($rowFromValues);

    $excelRows = array();
    foreach ($rows as $key => $row) {
        $row = (array) $row;
        $cells = array();
        foreach ($headArray as $keyc => $column) {
            $fieldType = $xmlObjectArray[$column]['type'];
            $col_name = $this->get_col_name($keyc);
            // set date formate
            if ($fieldType == "datetime") {
                if (! empty($row[$column])) {
                    // Get server time zone offset in seconds and add or subtract from main value
                    $timeZoneOffset = date('Z', $row[$column]);
                    $date = $row[$column] + $timeZoneOffset;
                    $date = Date::PHPToExcel(trim($date));
                    $cellstyle = (new StyleBuilder())->setFormat('dd mmm yyyy')->build();
                    $cells[] = WriterEntityFactory::createCell($date, $cellstyle);
                    
                } else {
                    
                    $cells[] = WriterEntityFactory::createCell('');
                }
            } elseif ($fieldType == "money") {
                if (strlen($row[$column]) > 0) {

                    $cellstyle = (new StyleBuilder())->setFormat('#,##0.00;[Red]#,##0.00')->build();
                    $cells[] = WriterEntityFactory::createCell($row[$column], $cellstyle);
                } else {
                    
                    $cells[] = WriterEntityFactory::createCell('');
                }
            }elseif ($fieldType == "number") {
                if (strlen($row[$column]) > 0) {

                    $cellstyle = (new StyleBuilder())->setFormat('#,##')->build();
                    $cells[] = WriterEntityFactory::createCell($row[$column], $cellstyle);
                } else {
                    
                    $cells[] = WriterEntityFactory::createCell('');
                }
            } elseif ($fieldType == "image") {
                $value = $row[$column];
                if (! empty($value)) {
                    $imageFolder = $this->config->item('img_upload_path');
                    $subDirectory = (string) $xmlObjectArray[$column]['sub_directory'];
                    if (isset($subDirectory) and ! empty($subDirectory)) {
                        $imageFolder .= $subDirectory . "/";
                    }
                    $filePath = $imageFolder . $value;
                    if (stripos($value, 'https://') !== false || stripos($value, 'http://') !== false) {

                        $cells[] = WriterEntityFactory::createCell('=HYPERLINK("'. $value .'","'. $value .'")');
                    } else {

                        $cells[] = WriterEntityFactory::createCell($value);
                    }
                }
                

            } elseif ($fieldType == "file") {

                $value = $row[$column];
                $imageFolder = $this->config->item('img_upload_path');
                if (! empty($value)) {
                    $value = CDN_URL . $imageFolder . $value;
                }
                
                $cells[] = WriterEntityFactory::createCell($value);
            } else {
                
                $cells[] = WriterEntityFactory::createCell($row[$column]);
            }
        }
        
        $excelRows[] = WriterEntityFactory::createRow($cells);
    }

    $writer->addRows($excelRows); 

    ob_clean();
    $writer->close();
    
    exit();
}
0

There are 0 answers