Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate OpenOffice calc files using PHP

I have been trying to find a simple way to create OpenOffice calc files with no success.

I have tried:

openTBS - Seems to work writing an xml and a template file but can't find anything about how the xml file format.

Ods php generator - I tried this one as it provides clear examples, but when I copy the files to my server I always get corrupted files

Php doc writer - Tried an example and got an sxw file. I don't even know what that is

ODS-PHP - No documentation, only one example for creating 4 cells

Everything looks old, stalled and undocumented. ¿Any suggestion?

like image 566
xzdead Avatar asked Nov 01 '22 06:11

xzdead


2 Answers

I have used opentbs successfully.

You can generate both excel and calc files. It also nice that you can "reuse" your html implementation so to speak.

Maybe this thread could get you going http://www.tinybutstrong.com/forum.php?thr=3069

Do the html version first.. then edit for calc/excel

like image 158
pernils Avatar answered Nov 15 '22 04:11

pernils


Spout from Box works well enough for me. There are some missing features but it is simple to use, has a fluent API, and has no dependencies (it supports composer but you can use it standalone and its dependency graph has zero depth 😉 ).

Here's my "array of objects to ODS" pipeline, using Spout: (I'm not using their recommended use import because all this code fits in a much larger file that I didn't want to contaminate and the $factory pattern looks cleaner to me anyway)

$factory = 'Box\Spout\Writer\Common\Creator\WriterEntityFactory';
$factory::createODSWriter()
    ->openToBrowser('filename.ods')
    ->addRow($factory::createRow([
        $factory::createCell(__('Heading 1')),
        $factory::createCell(__('Heading 2')),
        $factory::createCell(__('Heading 3')),
    ]))
    ->addRows(array_map(function($row) use ($factory) { 
        return $factory::createRow([
            $factory::createCell($row->first_val),
            $factory::createCell($row->second_val),
            $factory::createCell($row->third_val),
        ]);
    }, loadDataFromSomewhere()))
    ->close();

like image 24
Guss Avatar answered Nov 15 '22 05:11

Guss