box / spout

Read and write spreadsheet files (CSV, XLSX and ODS), in a fast and scalable way
http://opensource.box.com/spout/
Apache License 2.0
4.24k stars 637 forks source link

Support for DateTime when writing XLSX #662

Open stof opened 5 years ago

stof commented 5 years ago

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

mcorteel-harel commented 4 years ago

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.

schrieveslaach commented 4 years ago

735 adds this to ODT.

ChronicLogic commented 3 years ago

PR #751 adds XLSX.

adrilo commented 3 years ago

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.

mu1f407 commented 2 years ago

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.

Slamdunk commented 2 years ago

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

mcorteel-harel commented 2 years ago

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

Slamdunk commented 2 years ago

Released now in v3.6.0

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

mcorteel-harel commented 2 years ago

@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

Slamdunk commented 2 years ago
$style = (new StyleBuilder())
    ->setFormat('yyyy-mm-dd');
$cell = WriterEntityFactory::createCell(new \DateTime(), $style);
mcorteel-harel commented 2 years ago

:+1: Thanks a lot!