simple question here, I'm using PHPExcel to export a datagrid to an Excel file. In my datagrid, I have a few columns that contain dates and are formatted as 'Y-m-d'. When I extract the grid data, Excel does not recognize the data into those columns as Dates (Excel says that they have a General format and that they have no specific number format).
Is there anyway that I can format the dates so that Excel can recognize them as dates before the data is sent to Excel?
Thanks in advance
PHPExcel have in-built package to set number data as an excel date.
If you i got you right then you are just passing number value as an date and getting type error.
Try like: $date = PHPExcel_Style_NumberFormat::toFormattedString($data, "d-M-Y");
Or refer doc link for more details: http://www.cmsws.com/examples/applications/phpexcel/Documentation/API/PHPExcel_Style/PHPExcel_Style_NumberFormat.html#constFORMAT_DATE_DDMMYYYY
As described in the PHPExcel documentation and in the Examples such as 02types.php
You need to convert those "formatted" dates to an MS Excel serialized date/timestamp
$dateString = '2015-12-21';
$excelTimestamp = PHPExcel_Shared_Date::PHPToExcel($dateString);
and then set that $excelTimestamp as the cell value
$objPHPExcel->getActiveSheet()->setCellValue('C9', $excelTimestamp);
You should then set the format mask for the cell
$objPHPExcel->getActiveSheet()
->getStyle('C9')
->getNumberFormat()
->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_DATE_YYYYMMDD2);
Related
I use PhpSpreadsheet to create XLSX files. The problem is that I set the date inside the cells with a formatCode but when I create a chart based on these cells the formatCode is not applied on them. This leads to having timestamps inside the chart and a proper date format inside the cells. Does anyone know how to pass the right date format into the chart?
Best regards,
laobiz
You may need to apply date formatting to the cell, e.g. :
$worksheet->getStyleByColumnAndRow($column, $row)
->getNumberFormat()
->setFormatCode(\PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_DATE_DATETIME);
See supported date formats here.
Please note, that MS Excel does not work with unix timestamps like PHP (meaning the number of seconds since 01.01.1970). Instead, Excel counts
the number of days since 01.01.1900 and works with float values when converting to hours/minutes/secs. Therefore you cannot insert into a cell unix timestamp, but convert it into Excel timestamp before. See PHP conversion functions from and into Excel serial date format.
I am trying to format a excel column using PHPexcel as a custom date so that when user open it on MS excel 2007 the date on data sort is properly group see image.
I inspected the cell format and the format is below.
My PHP code is below.
$objPHPExcel->getActiveSheet()->getStyle('S:S')->getNumberFormat()->setFormatCode('m/d/yyyy h:mm');
But my output is below, Any advice how to achieve above format.
I am generating an excel sheet via phpExcel in which dates are retrieved in 01-JAN-1900 format.But I want it to be visible in DD-MM-YYYY format right from when the excel sheet is generated with default date values fetched from DB.Also the date format is DD-MM-YYYY in DB.So how can we format that cell to display the date in that particular format.
I am using this code.Then too,the DB fetched dates values are in 01-JAN-1900 format only. Please help.
for($i=2;$i<$count;$i++){
$objValidation = $objPHPExcel->getActiveSheet()
->getStyle('V'.$i)
->getNumberFormat()
->setFormatCode(
PHPExcel_Style_NumberFormat::FORMAT_DATE_DDMMYYYY
);
I am facing a problem with the PHPExcel class while i am using that class to read a excel file in my php code.
// Reading the excel data
$objReader = new PHPExcel_Reader_Excel2007();
$objReader->setReadDataOnly(true);
$objPHPExcel = $objReader->load('test.xlsx');
$objPHPExcel->setActiveSheetIndex(0);
$dataArray = $objPHPExcel->getActiveSheet()->toArray();
I am using this code, Now problem is that when I create a excel file and insert a cell value like a date say 01/09/2012
and reads the full excelsheet using the toarray() function of PHPExcel class, then it do not returns the date value. it's returning a numeric value.
What I found that when we create the Excel file using the MS OFfice application and insert a date into any cell the excel converts the cell to a date format. So now I want to read a date format cell using the PHPExcel class.
Rule one:
It's not a good idea to set $objReader->setReadDataOnly(true); when you'll be working with dates. If you do that, then PHPExcel cannot tell you whether a cell contains a date or a number.
Rule two:
That number is an Excel date value as stored by Excel. The Excel numberFormat Mask then is used to convert that number to a formatted "human readable" date at display-time. (If you've used $objReader->setReadDataOnly(true); then you haven't loaded the cell's numberFormat mask.
If you've loaded the numberFormat masks, then you can use the getFormattedValue() method to return the formatted "human-readable" date as a string.
Rule three:
To convert an Excel numeric date value to a unix date/time stamp or to a PHP DateTime object, you can use the built-in PHPExcel_Shared_Date::ExcelToPHP() or PHPExcel_Shared_Date::ExcelToPHPObject() methods.
Rule four:
All of this is fully documented. But I guess I'm going to have to write a special manual dedicated to Dates with the word DATES in big red letters in the filename.
I am using workbook and worksheet library to download excel sheet in our project. But this library write data in "text" or "general" format which is a problem. Most users want to sort the date so with the current format, the problem now is that they will be sorted alphabetically. Hence, please help me to format as in date so when sorted it will follow accordingly (oldest to newest or newest to oldest).
Is there any function or class in php that format date string (Like "24 Aug 2011") to actual date formate ?
Thanks a lot.
BiffWriter allows for writing dates as Excel date integers:
$excelDate = xlsDate(8,24,2011);
And then you can use this to write the data to your file:
xlsWriteNumber($row,$col,$excelDate);
Referenced from the BiffWriter docs.