Home » excel » php – How far can a .xlsx file be simplified

php – How far can a .xlsx file be simplified

Posted by: admin April 23, 2020 Leave a comment

Questions:

I’m currently working on a PHP project and would like to provide (very basic) Excel export of a database table (only strings and numbers in cells, no formatting, etc.). I have read up on the various Excel file formats. I would like to avoid including a library to do this.

I have considered using the legacy Microsoft Office XML format and it seems like the easiest solution. But Excel warns when I give the file an .xls extension instead of .xml. I don’t want to generate .xml files though, as I can not guarantee that users of my project will know not to simply double click the files and be confused. I’ll leave this as a final option.

Ideally, I’d like to generate a Office Open XML (.xlsx, aka. Excel Workbook) file that current versions of Excel open without complaining. So I looked into the format and found it’s a zip-compressed folder of .xml files.

structure of a xlsx file

I figured I’d just put the whole directory up on the server, write/replace xl/worksheets/sheet1.xml, zip it up again and output to the browser.
Sadly, the worksheet does not contain actual strings and numbers, but references all values from sharedStrings.xml.

I found this excellent answer https://stackoverflow.com/a/18820405/3625228 but ideally, I’d like to get rid of whatever files I can to simplify the .xlsx.

I looked into [Content_Types].xml, where the dependencies are stored:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types">
    <Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/>
    <Default Extension="xml" ContentType="application/xml"/>
    <Override PartName="/xl/workbook.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml"/>
    <Override PartName="/xl/worksheets/sheet1.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml"/>
    <Override PartName="/xl/theme/theme1.xml" ContentType="application/vnd.openxmlformats-officedocument.theme+xml"/>
    <Override PartName="/xl/styles.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml"/>
    <Override PartName="/xl/sharedStrings.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.sharedStrings+xml"/>
    <Override PartName="/docProps/core.xml" ContentType="application/vnd.openxmlformats-package.core-properties+xml"/>
    <Override PartName="/docProps/app.xml" ContentType="application/vnd.openxmlformats-officedocument.extended-properties+xml"/>
</Types>

My question is:

  • is it possible to remove files from the .xlsx archive?
  • What are the files that are absolutely needed for Excel to open without complaining?
  • And is there a way or a trick to put the actual strings/values into sheet1.xml and skip sharedStrings.xml altogether?

Disclaimer: Not looking for libraries and/or help using Microsoft Office XML (Excel 2003) format files

How to&Answers:

Yes, you can write directly to the sheet1.xml … I’m doing it like so: https://github.com/nimmneun/OneSheet
Maybe it gives you one or two ideas =)
I also don’t use a theme file btw.

update:
In case you’re still searching … I discovered that you actually only need 5 files, just make sure your rels point to the proper files/filenames:

workbook.xml
worksheet.xml
[Content_Types].xml
_rels/.rels
_rels/workbook.xml.rels

For reference: https://blogs.msdn.microsoft.com/brian_jones/2007/05/29/simple-spreadsheetml-file-part-3-formatting/

I linked part-3 b/c it has links to part 1 & 2 =)