I achieve this in a quick, somehow cheap way - because it is long and winding, I just explain it in concept, not in code.
XLSX adheres to the ISO 29500 standard, which is publicly available if you want to carefully manipulate your document in php. Otherwise, understand that xlsx files are zipped archives from a set of XML files.
Create the template that you need, say that it alternates lines with styles of different types, doing this in excel or in the open xml editor of some description. Make sure you put some data there, and make sure some fields are equal (for training purposes only).
Then save the file as xlsx, rename it .zip or open it in the archive extractor and view the contents.
First, pay attention to the file [Content_Types] .xml, this describes the location of the main files in the archive and the standards to which it adheres, and the types of contents of these files.
Everything outside the xl/ folder is really metadata. But observing docProps/core.xml contains information about the author, modification and docProps/core.xml , which you can replace in php when you recreate this file. Also, everything that docProps/core.xml can be renamed to your tastes, [Content_Types].xml cannot.
Okay, now you understand this, you will begin to observe the ides thrown around the place. They like to use this in a file format, everything refers to everything else with its index in a specific xml property list or similar. They also usually describe the number of items in such lists.
In xl/ you will see themes.xml, styles.xml, workbook.xml, sharedStrings.xml, _rels/, worksheets/ .
Styles will be inflated with a lot of unnecessary styles, which are built by default if you used it. But you should be able to see how these styles work so that you can customize your own.
Themes are pretty pointless to me, so I delete it and the identifiers that reference it.
Next, you will see a book that contains a file containing information about the sheets that are inside the spreadsheet document, since you can have more than 1, obviously. It also contains some sheet metadata, such as its size, etc.
Now comes the first big hua that you come across. sharedStrings.xml is a weird file that stores all the information that will be inserted into cells in a static spreadsheet. They are indexed, but the engine that reads the document determines what their indices are. Everything that is repeated can be returned back to its old index in the sheet itself (inside the worksheet folder) to save the file size in large documents with duplicate values. Not the count and uniquecount in the sst element and what they explicitly mean.
This is the stage in php where you fill in the data array containing what you want on your sheet and upload it to the list of formatted XML files, for example, this file appears. Also note that these files do not need to be clamped without newlines or newlines, since xml is still valid with or without them, and they will work regardless of readers.
Check the _rels folder, it will be fairly obvious.
Finally, this is the sheet itself. The numbers in the fields here refer to the indexed row locations in sharedStrings.xml . The attribute s is the style, t is the data type in the field. R is the location of the cell, but why is it needed, what is outside of me, when it can really be understood quite easily.
Creating this file in php should not be too complicated either. Just use your indexes from your dataset that you used to create your sharedStrings.xml file.
Oh, also in the sheet there is information about the column width, in which you can find based on the font you used, and automatically their size in php too, if necessary.
Finally, this is the packaging of everything in php.
My code is in a class that receives data and certain saved files that I created using excel to simplify it.
$this->folder_structure_simple = Array( "_rels/.rels" => "_rels__rels", "docProps/app.xml" => "docProps_app_xml", "docProps/core.xml" => "docProps_core_xml", "xl/_rels/workbook.xml.rels", "xl/theme/theme1.xml", "xl/worksheets/sheet1.xml", "xl/sharedStrings.xml", "xl/styles.xml", "xl/workbook.xml", "[Content_Types].xml" => "Content_Types_xml" ); $zip = new ZipArchive; $res = $zip->open($this->file_name, ZipArchive::CREATE); if($res === TRUE){ foreach($this->folder_structure_simple as $file => $function){ $zip->addFromString($file, $this->$funtion); } $zip->close(); echo 'ok'; }else{ return FALSE; }
And functions produce the required data. Very fast, not very flexible.