phpspreadsheet setCellValue is not writing

南楼画角 提交于 2019-12-24 01:47:19

问题


I am uploading a excel file, reading the contents and writing to a new excel file using phpspreadsheet.

I am trying to create a excel file.

I am writing into cells using the following code

$writesheet->setActiveSheetIndex(0)->setCellValue('A1','myemail@gmail.com');

these lines

        echo 'valid';
        echo $cell;
        echo $toemail;

print all the values correctly.

But this line of code is not working..

Full code:

<?php
include '../includes/common.php';
include "class.emailvalidator.php";






/////////////////////////////////////EXCEL READER ////////////////////////////////
if(!empty($_FILES)){
    require 'spreadsheet/vendor/autoload.php';
    $file = $_FILES['files']['tmp_name'];
    $file_name = $_FILES['files']['name'];
    $upload_path = 'files/'.$file_name;


    if(move_uploaded_file($file,$upload_path)){
        $spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($upload_path);
        $worksheet = $spreadsheet->getActiveSheet();
        $highestRow = $worksheet->getHighestRow();

        for ($row = 1; $row <= $highestRow; ++$row) {
            $toemail = $worksheet->getCellByColumnAndRow(1, $row)->getValue();
            $fromemail = 'admin@myimsuite.com';

            $details = verifyEmail($fromemail,$toemail,true);

            $writesheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();

            $cell = 'A'.$row;
            if($details[0]== 'valid'){
            echo 'valid';
            echo $cell;
            echo $toemail;
                $writesheet->setActiveSheetIndex(0)->setCellValue($cell, $toemail);
            }elseif($details[0]== 'invalid'){

            }


        }
        if(file_exists($upload_path)){
            unlink($upload_path);
        }
        $writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($writesheet);

        $fxls ='excel-file_1.xlsx';
        $writer->save($fxls);        
        if(file_exists($fxls)){
            //force_download($fxls);        
        }

    }


die();

}

   function force_download($filename) {
    $filedata = @file_get_contents($filename);

    // SUCCESS
    if ($filedata)
    {
        // GET A NAME FOR THE FILE
        $basename = basename($filename);

        // THESE HEADERS ARE USED ON ALL BROWSERS
        header("Content-Type: application-x/force-download");
        header("Content-Disposition: attachment; filename=$basename");
        header("Content-length: " . (string)(strlen($filedata)));
        header("Expires: ".gmdate("D, d M Y H:i:s", mktime(date("H")+2, date("i"), date("s"), date("m"), date("d"), date("Y")))." GMT");
        header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");

        // THIS HEADER MUST BE OMITTED FOR IE 6+
        if (FALSE === strpos($_SERVER["HTTP_USER_AGENT"], 'MSIE '))
        {
            header("Cache-Control: no-cache, must-revalidate");
        }

        // THIS IS THE LAST HEADER
        header("Pragma: no-cache");

        // FLUSH THE HEADERS TO THE BROWSER
        flush();

        // CAPTURE THE FILE IN THE OUTPUT BUFFERS - WILL BE FLUSHED AT SCRIPT END
        ob_start();
        echo $filedata;
    }

    // FAILURE
    else
    {
        die("ERROR: UNABLE TO OPEN $filename");
    }
}

/////////////////////////////////////EXCEL READER ////////////////////////////////



////////////////////////Email Related//////////////////////////////
//var_dump($log);

////////////////////////End Email Related//////////////////////////////
$objSmarty->assign("page",'bannerindex');

$objSmarty->setTemplateDir("../templates");
$objSmarty->display("email_validator.tpl");
?>

回答1:


Try running $writer->setPreCalculateFormulas(false); - fixed it for me



来源:https://stackoverflow.com/questions/50760172/phpspreadsheet-setcellvalue-is-not-writing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!