PHP Excel Templator (PHP template for Excel) or as we wrote hardcode for Excel before

image

I think that from the screenshot it is already clear what is being said. But, in order not to duplicate the text that is already described in the documentation , I will only talk about important points.

The fact is that the php-library for working with excel-documents appeared a long time ago, but so far it has not yet come up with a template engine (at least I haven’t found anything more sensible). And we all need such a library for excel, in which template variables are inserted into the template, passed parameters for them, and sent to render. All this is done by one command:

usealhimik1986\PhpExcelTemplator\PhpExcelTemplator;
PhpExcelTemplator::saveToFile('./template.xlsx', './exported_file.xlsx', [
	'{current_date}' => date('d-m-Y'),
	'{department}' => 'Sales department',
]);

In this case, we have the following Wishlist:

  • we want to apply several template variables in one cell
  • and we also want to transfer the array, and in excel, the corresponding strings were created in the cell for that
  • What about a two-dimensional array so that not only rows, but also columns are created
  • we also need to apply styles for each inserted cell, for example, to highlight large amounts of green or highlight distinguished employees in bold

You will say that I have already tried to do this: when rows are created, the columns to the right of this cell will be duplicated. No, this will not happen, because in reality no lines are created, but “cells are added with a shift down.” By the way, in PHP Spreadsheet at the moment there is no such function, it can only insert lines.

Are side effects possible when using a large number of one-dimensional and two-dimensional arrays? Yes, it is possible. Especially when using multiple two-dimensional arrays in the same row and on several lines in a row. The fact is that cells (which are on the right) that do not contain template variables, they do not move. As a result, they can be overlapped by cells with a two-dimensional array. The way out is to create template variables with empty data and place these variables next to the cells for which you want to indent. An example of side effects is available in the “samples” folder.

It sounds difficult, but if lines were created, there would be more side effects. That is, the chances of side effects are relatively low.

Plans: create an example that shows how to deal with side effects; the ability to apply styles without using setters (to export fewer classes and reduce the amount of code used). Why haven't I done it now? I need support, that is, I need to know that someone needs it. And then suddenly it turns out that I invented a bicycle and everything I did was complete game. Therefore, I am waiting for your feedback.

Link to the project

Also popular now: