spout icon indicating copy to clipboard operation
spout copied to clipboard

Support for DateTime when writing XLSX

Open stof opened this issue 6 years ago • 11 comments

The XLSX writer (and maybe also ODS, I haven't check) is missing support for date cells.

stof avatar Jun 27 '19 10:06 stof

There is an inconsistency in the code I think. In Box\Spout\Writer\XLSX\Manager\WorksheetManager::getCellXml and Box\Spout\Writer\ODS\Manager\WorksheetManager::getCellXml, the case elseif ($cell->isDate()) is not implemented, though Box\Spout\Common\Entity\Cell supports it. Also, when exporting to CSV, Cell::__toString() tries to cast a DateTime to a string, which is not possible and produces an exception.

mcorteel-harel avatar Feb 15 '20 12:02 mcorteel-harel

#735 adds this to ODT.

schrieveslaach avatar Mar 28 '20 11:03 schrieveslaach

PR #751 adds XLSX.

ChronicLogic avatar Dec 13 '20 03:12 ChronicLogic

There is an inconsistency in the code I think. In Box\Spout\Writer\XLSX\Manager\WorksheetManager::getCellXml and Box\Spout\Writer\ODS\Manager\WorksheetManager::getCellXml, the case elseif ($cell->isDate()) is not implemented, though Box\Spout\Common\Entity\Cell supports it. Also, when exporting to CSV, Cell::__toString() tries to cast a DateTime to a string, which is not possible and produces an exception.

This is true. Cell could support DateTime objects but full support is not ready, hence the error.

adrilo avatar May 14 '21 13:05 adrilo

Hi, I recently faced this issue of missing support for dates for XLSX in my project. I saw that there were some PRs for this. I think the nicest one was #816. For me this solution is great and it's according to specification. The fact that some clients (Google Docs was mentioned) don't support it doesn't bother me. This shouln't be the reason for not merging this functionality. It works in Excel, is according to spec and it doesn't break anything.

@adrilo Would it be possible to reconsider merge of this PR? Thanks.

mu1f407 avatar Jan 03 '22 18:01 mu1f407

Solved in https://github.com/openspout/openspout/pull/13

Slamdunk avatar Mar 03 '22 07:03 Slamdunk

@Slamdunk Thanks! Any idea when this will be released?

mcorteel-harel avatar Mar 03 '22 08:03 mcorteel-harel

Released now in v3.6.0

Be aware that you need to specify the number format to have the output you need.

Slamdunk avatar Mar 03 '22 08:03 Slamdunk

@Slamdunk Could you give an example? Would that work (seems weird)?

$style = (new StyleBuilder())
    ->setFormat('0.000')
    ->build();
$cell = WriterEntityFactory::createCell(new \DateTime(), $style);

EDIT: Missing build() method

mcorteel-harel avatar Mar 03 '22 08:03 mcorteel-harel

$style = (new StyleBuilder())
    ->setFormat('yyyy-mm-dd');
$cell = WriterEntityFactory::createCell(new \DateTime(), $style);

Slamdunk avatar Mar 03 '22 08:03 Slamdunk

:+1: Thanks a lot!

mcorteel-harel avatar Mar 03 '22 08:03 mcorteel-harel