phpspreadsheet setCellValue不写

时间:2018-06-08 11:58:33

标签: php phpspreadsheet

我正在上传excel文件,阅读内容并使用phpspreadsheet写入新的Excel文件。

我正在尝试创建一个excel文件。

我使用以下代码写入单元格

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

这些行

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

正确打印所有值。

但是这行代码不起作用..

完整代码:

<?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 个答案:

答案 0 :(得分:0)

尝试运行$writer->setPreCalculateFormulas(false);-为我修复了