How to create xlsx file without using any excel PHP library

I am using it right now.

$mimeType = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'; header('Content-Description: File Transfer'); header('Content-Type: ' . $mimeType); header('Content-Disposition: attachment; filename='.basename($type.'.xlsx')); header('Content-Transfer-Encoding: binary'); header('Expires: 0'); header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); header('Pragma: public'); print "$header\n$data"; exit; 

$header variable contains the excel header line to be generated, and looks like this:

$header= "Business_Name\tBusiness_Type\tType";

separated by \t

and $data contains the rows that should be generated in the header columns. They are also separated by the \t character, and the line ends with \n .

It loads with the current installation file, but it does not open with ms excel and displays this message.

Excel cannot open the file "file name" because the file format or file extension is invalid. Confirm that the file format has not been corrupted and that the file extension matches the file format.

What header should be sent to the server? or how can i generate this file?

+6
source share
3 answers

You have a CSV file. Depending on your OS, your browser and your version of Excel, the browser allows you to or not open CSV, XLS XLSX extensions using Excel software in different ways.

If you want your data to open using Excel, you can combine the data using an Excel template using OpenTBS . Use version 1.6.0 (or higher), which is currently in Release Candidate, because it provides basic features for Excel files.

There is a "no excel library PHP" in your title. I don’t know why you have this specification, but OpenTBS is not exactly an Excel library. This is a PHP tool for merging OpenOffice and Ms Office documents using templates.

+3
source

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.

+14
source

You have a CSV, not an .xlsx file. XLSX is a ZIP-wrapped XML block. Change your MIME type to text / csv.

+3
source

Source: https://habr.com/ru/post/888508/


All Articles