Author Topic: [SOLVED] Write to Excel  (Read 1561 times)

0 Members and 1 Guest are viewing this topic.

Offline oberTopic starter

  • Pandas pwn j00
  • Staff Alumni
  • Freak!
  • *
  • Posts: 8,312
  • Gender: Male
  • 404? what!?
    • View Profile
    • Windy Hill Productions
[SOLVED] Write to Excel
« on: July 05, 2008, 05:08:26 PM »
What do you guys use?  My servers are linux boxes and I'm curious how you guys handle Excel file creation.  Pear?  That stupid class on phpclasses.org?  Other?
PHP 5 - MySQL 5 - Win Vista 64 - Firefox 3, IE 7
Info: PHP Manual

Quote from CV: After nuclear fallout, there'll be zombies everywhere.  You can't be running from them when you're all fat and shit.  They'll just catch you and eat you and take forever to do it and you'll just have to sit there all that much longer.

Offline 448191

  • Staff Alumni
  • Fanatic
  • *
  • Posts: 3,506
  • Gender: Male
    • View Profile
Re: Write to Excel
« Reply #1 on: July 05, 2008, 09:33:46 PM »
I haven't tried it, but I was planning on trying out this lib at the next opportunity:

http://www.codeplex.com/PHPExcel

If you try it, let us know if you like it.

Offline neylitalo

  • Staff Alumni
  • Addict
  • *
  • Posts: 2,970
  • Gender: Male
    • View Profile
    • The Netizen's Journal
Re: Write to Excel
« Reply #2 on: July 05, 2008, 10:52:38 PM »
When I have to, I use the PEAR library. I really don't like it, though, because it can only generate Excel 97 files.
http://nealylitalo.net - My personal website, and home of The Netizen's Journal.

Offline bilis_money

  • Devotee
  • Posts: 619
  • Gender: Male
  • Tranquility.
    • View Profile
Re: Write to Excel
« Reply #3 on: July 06, 2008, 12:17:30 AM »
thanks for  the link 448191

Offline neylitalo

  • Staff Alumni
  • Addict
  • *
  • Posts: 2,970
  • Gender: Male
    • View Profile
    • The Netizen's Journal
Re: Write to Excel
« Reply #4 on: July 06, 2008, 01:54:44 AM »
If that PHPExcel lib works, I'll be thrilled. I don't plan on using it, though, as I try to stay away from Excel whenever possible.
http://nealylitalo.net - My personal website, and home of The Netizen's Journal.

Offline corbin

  • Guru
  • Freak!
  • *
  • Posts: 7,951
  • Gender: Male
    • View Profile
Re: Write to Excel
« Reply #5 on: July 06, 2008, 02:20:31 AM »
Does it definitely have to be an Excel file?  Can't just be a CSV file?  Guessing you need to modify an already existing Excel file....
Why doesn't anyone ever say hi, hey, or whad up world?

Offline 448191

  • Staff Alumni
  • Fanatic
  • *
  • Posts: 3,506
  • Gender: Male
    • View Profile
Re: Write to Excel
« Reply #6 on: July 06, 2008, 06:06:34 AM »
CSV files have some issues spoiled clients aren't ready to deal with. They expect to simply choose "open with Excel" and it'll work.

- Formatting options (seperator, boundraries)
   You have to know what options are default on the clients system. Otherwise they have to do "text to columns" or import.

- Character encoding
  Again, you need to know what the default is on the clients system. Otherwise they have to do import and choose the right encoding.
  If your data is in UTF8, they will definitely have to do this, or you have to convert the source data.

In all, using CSV is quite a bit more trouble than one would think. Generating Excel files is less hassle.

Not to mention Excel sheets will allow a plenitude of extra options such as multiple sheets and data input validation (which would be extremely useful for imports).

You know what, I think I'll give it a quick go later today. :)

Offline 448191

  • Staff Alumni
  • Fanatic
  • *
  • Posts: 3,506
  • Gender: Male
    • View Profile
Re: Write to Excel
« Reply #7 on: July 06, 2008, 06:53:51 AM »
Ok, I had little play with the test scripts, and in all honesty, it looks pretty damn good. It is however very expensive on memory and processor time. Also it has quite serious requirements (I don't quite see why the Zip extension is required, but there's probably a good reason for it).

Bellow script creates a very basic file. It has a peak memory usage of 7.75 MB and took 0.451s to complete.

$objPHPExcel = new PHPExcel();

// Set properties
$objPHPExcel->getProperties()->setCreator("Maarten Balliauw");
$objPHPExcel->getProperties()->setLastModifiedBy("Maarten Balliauw");
$objPHPExcel->getProperties()->setTitle("Office 2007 XLSX Test Document");
$objPHPExcel->getProperties()->setSubject("Office 2007 XLSX Test Document");
$objPHPExcel->getProperties()->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.");
$objPHPExcel->getProperties()->setKeywords("office 2007 openxml php");
$objPHPExcel->getProperties()->setCategory("Test result file");


$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->setCellValue('A1''Hello');
$objPHPExcel->getActiveSheet()->setCellValue('B2''world!');
$objPHPExcel->getActiveSheet()->setCellValue('C1''Hello');
$objPHPExcel->getActiveSheet()->setCellValue('D2''world!');

// Rename sheet
$objPHPExcel->getActiveSheet()->setTitle('Simple');


// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);

	
	

// Save Excel 2007 file
$objWriter PHPExcel_IOFactory::createWriter($objPHPExcel'Excel2007');
$objWriter->save('C:\Documents and Settings\John\Bureaublad\1.6.2\test.xlsx');


Writing an Excel5 file replacing the createWriter statement with:

$objWriter PHPExcel_IOFactory::createWriter($objPHPExcel'Excel5');

Uses 8.25 MB and takes 0.4828.

File attached.

The memory usage stays about the same doing more complicated stuff, but processing time quickly skyrockets (and this is on a 'very fast' system).

Doing more complicated things apparently requires A LOT of coding. You have to apply stuff manually on every cell. Example:

$objPHPExcel->getActiveSheet()->getStyle('E11')->getBorders()->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);

Interface could be better. :/ Probably if I ever need to do something like this I would create an interface to apply styles on ranges.

I would post the code, but it's absolutely huge. It's included in the distribution anyway.

Creating this file took just over 8 seconds..  :o

NOTE: I had rename the files from .xlsx to .txt to be able to upload them to the forums.


[attachment deleted by admin]
« Last Edit: July 06, 2008, 06:56:24 AM by 448191 »

Offline oberTopic starter

  • Pandas pwn j00
  • Staff Alumni
  • Freak!
  • *
  • Posts: 8,312
  • Gender: Male
  • 404? what!?
    • View Profile
    • Windy Hill Productions
Re: Write to Excel
« Reply #8 on: July 06, 2008, 09:04:23 PM »
Thanks for the code John... I'll give that a whirl.  Much appreciated.  :)
PHP 5 - MySQL 5 - Win Vista 64 - Firefox 3, IE 7
Info: PHP Manual

Quote from CV: After nuclear fallout, there'll be zombies everywhere.  You can't be running from them when you're all fat and shit.  They'll just catch you and eat you and take forever to do it and you'll just have to sit there all that much longer.

Offline corbin

  • Guru
  • Freak!
  • *
  • Posts: 7,951
  • Gender: Male
    • View Profile
Re: Write to Excel
« Reply #9 on: July 07, 2008, 12:49:25 AM »
Nice!  And good reasons not to use CSV.... ;p
Why doesn't anyone ever say hi, hey, or whad up world?

Offline 448191

  • Staff Alumni
  • Fanatic
  • *
  • Posts: 3,506
  • Gender: Male
    • View Profile
Re: Write to Excel
« Reply #10 on: July 07, 2008, 03:43:37 AM »
Thanks for the code John... I'll give that a whirl.  Much appreciated.  :)

You're welcome, but I didn't code that ;) Just part of my mini review.

It is included in the distro.

Offline oberTopic starter

  • Pandas pwn j00
  • Staff Alumni
  • Freak!
  • *
  • Posts: 8,312
  • Gender: Male
  • 404? what!?
    • View Profile
    • Windy Hill Productions
Re: Write to Excel
« Reply #11 on: July 07, 2008, 04:04:10 PM »
Yeah... I noticed they hand a bunch of code sitting on the site after I visited it.  Now I just think you're a cheater ;)
PHP 5 - MySQL 5 - Win Vista 64 - Firefox 3, IE 7
Info: PHP Manual

Quote from CV: After nuclear fallout, there'll be zombies everywhere.  You can't be running from them when you're all fat and shit.  They'll just catch you and eat you and take forever to do it and you'll just have to sit there all that much longer.

Offline oberTopic starter

  • Pandas pwn j00
  • Staff Alumni
  • Freak!
  • *
  • Posts: 8,312
  • Gender: Male
  • 404? what!?
    • View Profile
    • Windy Hill Productions
Re: [SOLVED] Write to Excel
« Reply #12 on: August 24, 2008, 02:05:32 PM »
I just wanted to follow up on this thread.  I just started using this to generate some reports for a client and it is fantastic.  I agree that the styling interface could be simplified, but I'll take what I can get.  Very powerful and easy to setup and use.
PHP 5 - MySQL 5 - Win Vista 64 - Firefox 3, IE 7
Info: PHP Manual

Quote from CV: After nuclear fallout, there'll be zombies everywhere.  You can't be running from them when you're all fat and shit.  They'll just catch you and eat you and take forever to do it and you'll just have to sit there all that much longer.