PEAR Forum :: PHP Extension and Application Repository

PEAR Forum Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
Problem generating excel files

 
Post new topic   Reply to topic    PEAR Forum Forum Index -> What do you miss in/about PEAR?
View previous topic :: View next topic  
Author Message
striker170



Joined: 24 Oct 2008
Posts: 5

PostPosted: Sat Oct 25, 2008 3:09 am    Post subject: Problem generating excel files Reply with quote

I wish I could have put this in the "General PEAR Questions" section of the forum, but it's locked, so here goes:

I have a database webserver on my local network and I want to be able to generate excel files using data fetched from queries to the SQL server.

Here is the code I'm using to create a test file:

<?php
require_once 'Writer.php';

$workbook = new Spreadsheet_Excel_Writer();
//$workbook->send('test1.xls');

$worksheet =& $workbook->addWorksheet("Worksheet_1");

$worksheet->write(0, 0, 'Name');
$worksheet->write(0, 1, 'Age');
$worksheet->write(1, 0, 'John Smith');
$worksheet->write(1, 1, 30);
$worksheet->write(2, 0, 'Johann Schmidt');
$worksheet->write(2, 1, 31);
$worksheet->write(3, 0, 'Juan Herrera');
$worksheet->write(3, 1, 32);

$workbook->close();
?>

It seems to work fine when the line with the send() function is commented out. The excel file is created and saved locally on my webserver. However, when I un-comment the send function in an attempt to download the excel file to my local development machine, I get a file that MS Excel doesn't read properly. For example, here are the contents of cell A1:

ࡱ;

Can anyone help me?
Back to top
View user's profile Send private message AIM Address
mark



Joined: 07 Jan 2007
Posts: 1235

PostPosted: Sat Oct 25, 2008 3:49 pm    Post subject: Reply with quote

Your code example works fine here, both by sending it to the browser and by writing a file. The require_once call should look this if you have a proper installation and if have configured the include_path correctly:

Code:
require_once 'Spreadsheet/Excel/Writer.php';


The mentioned content of cell A1 is actually the beginning of the binary content of the file itself. Did you maybe change something else, and did you not only remove the send() call (and add the desired filename to the constructor)?
Back to top
View user's profile Send private message
striker170



Joined: 24 Oct 2008
Posts: 5

PostPosted: Mon Oct 27, 2008 6:50 pm    Post subject: Reply with quote

I did remove the filename from the constructor when I removed the send call.

The only other thing I've changed at all is the file structure: my test file is in the same folder as Writer.php, hence the difference in the require_once call. I don't have a Spreadsheet or Excel folder: I put everything in a folder called Writer and adjusted the require_once calls accordingly.

I should probably also mention that the output file wasn't purely binary: it had words like 'Name' and 'Age' in there, but they were surrounded by binary junk.

Why would I get binary formatting when sending to my browser and get pure ASCII when just saving to the server?
Back to top
View user's profile Send private message AIM Address
striker170



Joined: 24 Oct 2008
Posts: 5

PostPosted: Mon Oct 27, 2008 9:19 pm    Post subject: Reply with quote

Ok, I've figured out what's wrong:

I looked at the excel file generated locally on my server (the good file) and the excel file that saved to my local machine through my browser (the bad file).

I compared the two, viewing the files in hexadecimal and literally the only difference between the two is that in the bad file, there's a 0D0A (\r\n) added as the first two characters in the file.

So now that I know this, how do I fix it?
Back to top
View user's profile Send private message AIM Address
mark



Joined: 07 Jan 2007
Posts: 1235

PostPosted: Mon Oct 27, 2008 10:48 pm    Post subject: Reply with quote

Please fix your installation first. It makes no sense to try further steps with a "broken" (read: non-standard) installation of Spreadsheet_Excel_Writer and OLE.
Back to top
View user's profile Send private message
mark



Joined: 07 Jan 2007
Posts: 1235

PostPosted: Mon Oct 27, 2008 11:00 pm    Post subject: Reply with quote

My last reply was meant to your first reply. I hadn't seen your new reply. But the new reply indicates that you maybe added a newline while you edited the SEW/OLE files -- something that is not needed and not recommended.
Back to top
View user's profile Send private message
striker170



Joined: 24 Oct 2008
Posts: 5

PostPosted: Tue Oct 28, 2008 9:06 pm    Post subject: Reply with quote

Ok -- I removed the entire PEAR installation directory, re-installed, and put SEW/OLE in their proper place. Currently, the only file I've edited is test.php (from above). I'm still having the same problem: "0D0A" characters are added as the first bytes of the file.
Back to top
View user's profile Send private message AIM Address
striker170



Joined: 24 Oct 2008
Posts: 5

PostPosted: Wed Oct 29, 2008 2:46 am    Post subject: Reply with quote

FINALLY fixed this problem. I had one line of whitespace at the top of my .php file before the <?php tag, which caused my whole problem. I hope someone else reads this and avoids my headache.
Back to top
View user's profile Send private message AIM Address
Display posts from previous:   
Post new topic   Reply to topic    PEAR Forum Forum Index -> What do you miss in/about PEAR? All times are GMT + 2 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum



PEAR Forum topic RSS feed 
Powered by phpBB © 2001, 2005 phpBB Group

Provided by Ministry of Web developement