|
If you ever write some HTTP endpoint where tabulated data is returned, you could quite reasonably return RFC 4180 style CSV. However, if your API ever interfaces with users in a corporate environment, parsing simple comma-separated UTF-8 CSV is suddenly quite beyond the reach of however is nibbling at your endpoint, so why not code up a simple little reusable bit of code where you can write any simple tabular data (string, numbers, and dates, in one or more sheets of data made up of rows and columns) that lets you choose the output format? A zip-archive of CSV-files (one per sheet), JSON, ODS, or XLSX; pick your poison. I did just that, and while it is perfectly doable, any low-level, low-resources, low-dependency approach will mean actually touching the XML in LibreOffice's ODS (fine), and Microsoft's OOXML (…). This is how you write a date in a cell in both. ODS: <table:table-row table:style-name="ro1">
<table:table-cell office:value-type="date" office:date-value="2021-04-10T12:34:56" calcext:value-type="date">
<text:p>10/4/2021, 12:34</text:p>
</table:table-cell>
</table:table-row>
OK, a bit verbose, but trivial to implement. Format the date however you like — you'll probably use two different formatters on the same datetime instant.XLSX (OOXML): <row r="1" ht="12.8">
<c r="A1" s="1" t="n">
<v>39448.5</v>
</c>
</row>
Obviously, as you can all plainly see, the date here is 2008-01-01T12:00:00…And of course it makes perfect sense to hardcode the cell coordinate there. It's not like you would dynamically generate a bunch of cells (…). |
Excel can directly ingest a CSV file served over an URL as data source, with the Accept header manually set to text/csv.
I wrote a backend once that supported this feature so that management could pull whatever data they wanted off an internal application without pestering me. They could literally take the URL of a page and pull it as a CSV file as-is.
Anybody who knows a bit of Excel can pull that data themselves by following a set of simple instructions.