【问题标题】:Setting a cell with a date format in phpspreadSheet在 phpspreadSheet 中设置具有日期格式的单元格
【发布时间】:2018-09-07 08:12:59
【问题描述】:

我正在尝试以这种方式在 xslx(它只是一个测试脚本)文件中设置一些具有日期格式的单元格

<?php
    require 'vendor/autoload.php';

    use PhpOffice\PhpSpreadsheet\Spreadsheet;
    use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
    \PhpOffice\PhpSpreadsheet\Cell\Cell::setValueBinder( new \PhpOffice\PhpSpreadsheet\Cell\AdvancedValueBinder() );

    $spreadsheet = new Spreadsheet();
    $sheet = $spreadsheet->getActiveSheet();
    $sheet->setCellValue('A2',2);
    $sheet->setCellValue('A3',3);
    $sheet->setCellValue('A4',4);
    $sheet->setCellValue('A5',5);

    $time = gmmktime(0,0,0,12,31,2008);
    $sheet->setCellValue('B2', \PhpOffice\PhpSpreadsheet\Shared\Date::PHPToExceldate($time));
    $sheet->setCellValue('B3', \PhpOffice\PhpSpreadsheet\Shared\Date::PHPToExceldate($time));
    $sheet->setCellValue('B4', \PhpOffice\PhpSpreadsheet\Shared\Date::PHPToExceldate($time));
    $sheet->setCellValue('B5', \PhpOffice\PhpSpreadsheet\Shared\Date::PHPToExceldate($time));

    $writer = new Xlsx($spreadsheet);

    if(file_exists('prueba.xlsx'))
        unlink('prueba.xlsx');


    $writer->save('prueba.xlsx');

但我收到此错误消息

致命错误:在第 16 行调用 C:\xampp\htdocs\prueba_excel\index.php 中未定义的方法 PhpOffice\PhpSpreadsheet\Shared\Date::PHPToExceldate()

我已经包含了自动加载文件,我有什么遗漏吗?

【问题讨论】:

  • 您正在调用的函数 - Date::PHPToExceldate - 在 Date 类中不存在。试试这个函数:public static function PHPToExcel($dateValue)
  • @lovelace 我知道它没有找到函数,但我使用的是文档示例,我遇到了同样的问题

标签: php phpspreadsheet


【解决方案1】:

您需要将 use PhpOffice\PhpSpreadsheet\Spreadsheet; 放在 php 脚本的顶部,这样:

<?php
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-10-20
    • 1970-01-01
    • 1970-01-01
    • 2020-12-14
    • 2017-11-02
    • 2022-01-09
    • 1970-01-01
    相关资源
    最近更新 更多