Excel export from Admin backend

280 views
Skip to first unread message

Sergei Sokolov

unread,
May 20, 2011, 7:50:42 AM5/20/11
to Joomla! General Development
Hello Joomla gurus!

Making a component that should be able to export data to Excel .xlsx
format from admin backend,
I ran into a deep forest:

1. PHPExcel (http://phpexcel.codeplex.com/) works as a standalone
script, but when ported to component's MVC controller task, it outputs
unreadable Excel file. Sscript's and Task's results looks similar at
the first sight, at least their start and end. What could be a reason,
I wonder?
Worth noticing: if you try to use the PHPExcel class with Joomla, a
core hack would be necessary. Read here: http://phpexcel.codeplex.com/discussions/211925

2. The whole approach to a admin task rendering a raw output - what's
the best practice here? I just set headers, and echo the content -
right? Or should there be some magic with buffers, output encoding
etc.?

Ideas, hints and links are welcome! : )


Serge.

Omar Ramos

unread,
May 20, 2011, 1:09:10 PM5/20/11
to joomla-de...@googlegroups.com
Hi Serge,

I'm using something like the following in my controller's to output the file:
        // Use header below for Excel 2007
        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        header('Content-Disposition: attachment;filename="' . $filename . '"');
        header('Cache-Control: max-age=0');

        $objWriter->save('php://output');
        JFactory::getApplication()->close();

I believe I'm using the 1.7.2 version at the moment so luckily I never ran into the issue you linked to.

-Omar


--
You received this message because you are subscribed to the Google Groups "Joomla! General Development" group.
To post to this group, send an email to joomla-de...@googlegroups.com.
To unsubscribe from this group, send email to joomla-dev-gene...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/joomla-dev-general?hl=en-GB.


Sam Moffatt

unread,
May 21, 2011, 3:12:05 AM5/21/11
to joomla-de...@googlegroups.com
Joomla! 1.6 changes the autoload behaviour to use the SPL function as
Joomla! 1.6 requires PHP 5.2 to be used. Joomla! 1.5 doesn't utilise
this functionality because spl_autoload_register was only introduced
in PHP 5.1.2 and Joomla! 1.5 supports back to PHP 4.3.10.

You don't need to hack the core, just put the following line after
where you include PHPExcel or even within your copy of PHPExcel:
spl_autoload_register(array('JLoader','load'));

I don't understand why people have this blind obsession with hacking
things for reasons they clearly misunderstand when they can easily
resolve it without having to hack things. Perhaps they like making
more work for themselves.

What Omar has suggested seems reasonable.

Sam Moffatt
http://pasamio.id.au

Sergiks

unread,
May 21, 2011, 9:34:42 AM5/21/11
to Joomla! General Development
Thanks Omar!
It works! The call to exit() makes the difference.

Sam, thanks for the advice. Tryed it under J1.5, got the Fatal error:
Fatal error: Uncaught exception 'LogicException' with message 'Passed
array specifies a non static method but no object (non-static method
JLoader::load() should not be called statically)' in /Users/.../
administrator/components/com_.../excel/PHPExcel.php:3


Serge.

Sam Moffatt

unread,
May 21, 2011, 9:41:27 AM5/21/11
to joomla-de...@googlegroups.com
Ahh of course, that explains the dummy function being created. Will be
very happy once 1.5 is dead and we're not supporting PHP 4.3 any more.

Just means it's (unfortunately) slightly more long winded like
specified in said thread but you again just put it where you include
or in their library. 1.6 however should resolve this though so no need
for the work around.

Cheers,

Sam Moffatt
http://pasamio.id.au

shumisha

unread,
May 21, 2011, 10:32:30 AM5/21/11
to Joomla! General Development
hi

Actually, I think the problem is more in phpexcel. __autoload() is
(was) a standard feature of php4 and you might expect to break some
PHP 4 application if you simply use spl_autoload_register the way
phpexcel seems to be doing.
A safer way of doing it is:

// get Joomla autloader out
spl_autoload_unregister("__autoload");

// register your own
$registered = spl_autoload_register(array('MyautoloaderClass',
'doAutoload'));

// put back existing, PHP4 autoloading function
if(function_exists('__autoload')) {
spl_autoload_register('__autoload');
}

As Sam put it, no need to hack anything

Now when I think about it, wouldn't it be safer to have Joomla 1.6+ be
use the same method ? Sam ?

Rgds

On May 21, 3:41 pm, Sam Moffatt <pasa...@gmail.com> wrote:
> Ahh of course, that explains the dummy function being created. Will be
> very happy once 1.5 is dead and we're not supporting PHP 4.3 any more.
>
> Just means it's (unfortunately) slightly more long winded like
> specified in said thread but you again just put it where you include
> or in their library. 1.6 however should resolve this though so no need
> for the work around.
>
> Cheers,
>
> Sam Moffatthttp://pasamio.id.au

Sam Moffatt

unread,
May 21, 2011, 11:26:24 PM5/21/11
to joomla-de...@googlegroups.com
Joomla! is particularly unfriendly towards any sort of other system
which would likely suffer the eradication of its environment upon
loading Joomla! short of them specifying a special define upon loading
Joomla! (added in 1.6). The cleaning of the environment is a security
measure but has the net effect of wiping out globals variables defined
prior to its inclusion (hence the use of constants in places as well).
Joomla! 1.5 only features it due to the misfortune of having to
support PHP4, PHP 5.0 and 5.1 before 5.1.2. Joomla! 1.6 is explicitly
PHP 5.2 so I'd expect people to use the new mechanisms for loading
rather than the old one.

Cheers,

Sam Moffatt
http://pasamio.id.au

shumisha

unread,
May 23, 2011, 3:52:40 AM5/23/11
to Joomla! General Development
Hi Sam,

It's rather the other way around I had in mind. It's not uncommon to
integrate within J! exactly as PHPExcel is used here.
Cleaning up globlas is usually not an issue, as request vars are left
untouched.
The suggestion is simply to add this layer of caution, so as not to
break scripts that could use the older __autoload() function instead
of the current spl_autoload_register.
Does not cost us anything, and could make life easier for some people.

cheers

On May 22, 5:26 am, Sam Moffatt <pasa...@gmail.com> wrote:
> Joomla! is particularly unfriendly towards any sort of other system
> which would likely suffer the eradication of its environment upon
> loading Joomla! short of them specifying a special define upon loading
> Joomla! (added in 1.6). The cleaning of the environment is a security
> measure but has the net effect of wiping out globals variables defined
> prior to its inclusion (hence the use of constants in places as well).
> Joomla! 1.5 only features it due to the misfortune of having to
> support PHP4, PHP 5.0 and 5.1 before 5.1.2. Joomla! 1.6 is explicitly
> PHP 5.2 so I'd expect people to use the new mechanisms for loading
> rather than the old one.
>
> Cheers,
>
> Sam Moffatthttp://pasamio.id.au

Sam Moffatt

unread,
May 23, 2011, 3:58:24 AM5/23/11
to joomla-de...@googlegroups.com
Integrate with in J!? I'm not quite sure I'm following - you're using
the Joomla! framework inside a third party script?

Cheers,

Sam Moffatt
http://pasamio.id.au

shumisha

unread,
May 23, 2011, 10:57:52 AM5/23/11
to Joomla! General Development
No, maybe that was unclear. I am talking about using external scripts
inside a J! component.
From your previous message I understood you yourself were talking
about integrating J! into another scrip because otherwise I don't see
how wiping out globals would affect the operation of an integrated
script (as the wiping out happens very early in the request
processing, at a time when such integrated script does not have yet
setup any globals). I think you can still get Wordpress for Joomla for
instance.

So what I'm saying is:
1 - it is not uncommon to integrate an external scrip into J!, usually
inside a "wrapping" component
2 - if such script uses __autoload, it'll be broken by the way J! uses
spl_register_autoload
3 - the couple of lines above allows said script to run w/o issue -
well at least no autoloading issue



On May 23, 9:58 am, Sam Moffatt <pasa...@gmail.com> wrote:
> Integrate with in J!? I'm not quite sure I'm following - you're using
> the Joomla! framework inside a third party script?
>
> Cheers,
>
> Sam Moffatthttp://pasamio.id.au
Reply all
Reply to author
Forward
0 new messages