问题描述
我正在尝试使用PHPExcel将日期插入单元格中.这是我的代码:
I'm trying to insert date into the cell using PHPExcel. This is my code:
include('xlsx/Classes/PHPExcel.php');
include('xlsx/Classes/PHPExcel/Calculation.php');
include('xlsx/Classes/PHPExcel/Cell.php');
$objPHPExcel = new PHPExcel();
$start = 3;
$objPHPExcel->getActiveSheet()->getStyle('A1')->getNumberFormat()->setFormatCode('DD.MM.YYYY');
$objPHPExcel->getActiveSheet()->SetCellValue('A1', date('d.m.Y', time()+60*($start + 3)));
$objPHPExcel->getActiveSheet()->getStyle('A1')->getNumberFormat()->setFormatCode('DD.MM.YYYY');
$objWriter = new PHPExcel_Writer_Excel5($objPHPExcel);
$objWriter->save('text.xls');
但是,PHPexcel选择正确的格式,但是它用单引号插入日期
However, PHPexcel choose right format, but it inserts date with single quote
'27.02.2016
代替
27.02.2016
我不知道如何在没有引号的情况下插入日期,希望您能对我有所帮助.
I don't know how to insert date without this quote, hope you help me.
推荐答案
setFormatCode
方法设置Excel将用于输出日期的格式,而不是为PHPExcel设置用于解释日期的格式. Excel电子表格以特定的时间戳格式存储日期,可以使用PHPExcel_Shared_Date
类生成日期.它具有在各种日期格式和Excel的预期格式之间进行转换的方法.
The setFormatCode
method sets the format Excel will use to output the date, rather than setting a format for PHPExcel to interpret the date. Excel spreadsheets store dates in a specific timestamp format which can be generated using the PHPExcel_Shared_Date
class. It has methods for converting between various date formats and Excel's expected format.
$objPHPExcel->getActiveSheet()->SetCellValue('A1',
PHPExcel_Shared_Date::PHPToExcel(time()+60*($start + 3)));
...应该可以解决问题.
...should do the trick.
这篇关于为什么PHPexcel在日期字段中插入单引号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!