php PHPExcel:如何为单元格设置日期格式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38933312/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 01:46:14  来源:igfitidea点击:

PHPExcel: how to set date format for a cell

phpdateformattingphpexcel

提问by user4035

I need to save a date to Excel file, it must be output in format "dd/mm/yyyy" (or the local date format of the user), and to be treated as a date so a column of them could be sorted correctly.

我需要将日期保存到 Excel 文件中,它必须以“dd/mm/yyyy”格式(或用户的本地日期格式)输出,并被视为日期,以便对其中的一列进行正确排序.

Here is the code:

这是代码:

<?php
include_once("../PHPExcel/Classes/PHPExcel.php");
date_default_timezone_set('Europe/London');
$objPHPExcel = new PHPExcel();
$cacheMethod = PHPExcel_CachedObjectStorageFactory::cache_in_memory_gzip;
PHPExcel_Settings::setCacheStorageMethod($cacheMethod);

$objPHPExcel = new PHPExcel();
$sheet = $objPHPExcel->getActiveSheet();
PHPExcel_Shared_Font::setAutoSizeMethod(PHPExcel_Shared_Font::AUTOSIZE_METHOD_EXACT);

//I didn't find dd/mm/yyyy format, so I used yyyy-mm-dd
$sheet->setCellValueByColumnAndRow(0, 1, "2014-10-16");
$sheet->getStyleByColumnAndRow(0, 1)
    ->getNumberFormat()->setFormatCode(
        PHPExcel_Style_NumberFormat::FORMAT_DATE_YYYYMMDD2
);

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save("test.xlsx");

It creates a file, but instead of my local date format I see: "2014-10-16" and the format of the cell is "All formats" -> "yyyy-mm-dd". I wanted it to be parsed and output in my local date format.

它创建了一个文件,但我看到的不是我的本地日期格式:“2014-10-16”,单元格的格式是“所有格式”->“yyyy-mm-dd”。我希望它以我的本地日期格式进行解析和输出。

I looked into the source code of PHPExcel/Classes/PHPExcel/Style/NumberFormat.php, and found many date formats:

我查看了 的源代码PHPExcel/Classes/PHPExcel/Style/NumberFormat.php,发现了许多日期格式:

const FORMAT_DATE_YYYYMMDD2 = 'yyyy-mm-dd';
const FORMAT_DATE_YYYYMMDD = 'yy-mm-dd';
const FORMAT_DATE_DDMMYYYY = 'dd/mm/yy';
const FORMAT_DATE_DMYSLASH = 'd/m/y';
const FORMAT_DATE_DMYMINUS = 'd-m-y';
...

But I am not sure what to use. How can I achieve the desired goal?

但我不确定要使用什么。我怎样才能达到预期的目标?

回答by Mark Baker

$sheet->setCellValueByColumnAndRow(0, 1, "2014-10-16");

Sets a stringvalue in the cell, not a date. Just because you interpret that as a date, doesn't mean that computer programs automagically interpret it as a date.

在单元格中设置字符串值,而不是日期。仅仅因为您将其解释为日期,并不意味着计算机程序会自动将其解释为日期。

Look at the date Examples in the PHPExcel Documentationand Examples, and you'll see that you need to set the cell value to a MS Excel serialized timestamp (a float value of the number of days since 1st January 1900). You can use the PHPExcel functions like PHPExcel_Shared_Date::PHPToExcel()to convert human dates/PHP DateTime objects/Unix timestamps to MS Excel Serialized timestamps.

查看 PHPExcel文档示例中的日期示例,您将看到您需要将单元格值设置为 MS Excel 序列化时间戳(自 1900 年 1 月 1 日以来的天数的浮点值)。您可以使用 PHPExcel 函数PHPExcel_Shared_Date::PHPToExcel()将人类日期/PHP DateTime 对象/Unix 时间戳转换为 MS Excel 序列化时间戳。

$sheet->setCellValueByColumnAndRow(0, 1, PHPExcel_Shared_Date::PHPToExcel( '2014-10-16' ));

Once you've stored the value as a timestamp, you can then apply whatever date format mask you want to that cell to get your desired formatting

将值存储为时间戳后,您可以将所需的任何日期格式掩码应用于该单元格以获得所需的格式

回答by user4035

This code generates 4 cells in date format.

此代码以日期格式生成 4 个单元格。

<?php
include_once("../PHPExcel/Classes/PHPExcel.php");
date_default_timezone_set('Europe/London');
$objPHPExcel = new PHPExcel();
$cacheMethod = PHPExcel_CachedObjectStorageFactory::cache_in_memory_gzip;
PHPExcel_Settings::setCacheStorageMethod($cacheMethod);

$objPHPExcel = new PHPExcel();
$sheet = $objPHPExcel->getActiveSheet();
PHPExcel_Shared_Font::setAutoSizeMethod(PHPExcel_Shared_Font::AUTOSIZE_METHOD_EXACT);

$format = 'dd/mm/yyyy';
for ($i = 1; $i < 5; ++$i)
{
    $date = new DateTime('2016-12-0'.$i);
    $sheet->setCellValueByColumnAndRow(0, $i, 
                                       PHPExcel_Shared_Date::PHPToExcel( $date ));

    $sheet->getStyleByColumnAndRow(0, $i)
        ->getNumberFormat()->setFormatCode($format);
}

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save("test.xlsx");