|
Posted by Jerry Stuckle on November 26, 2008, 11:28 pm
Please log in for more thread options
sheldonlg wrote:
show/hide quoted text
> Jerry Stuckle wrote:
>> sheldonlg wrote:
>>> Jerry Stuckle wrote:
>>>> sheldonlg wrote:
>>>>> sheldonlg wrote:
>>>>>> me wrote:
>>>>>>> I need to get some data from a MySQL database into an Excel
>>>>>>> Spreadsheet, potentially using PHP.
>>>>>>> What's available in PHP to help me with writing the MS-Excel
>>>>>>> file? Thanks,
>>>>>> I have an excel class that I downloaded from somewhere and
>>>>>> modified to my own tastes. It is limited to being able to put up
>>>>>> only one block of data, with a header row and a title row. If you
>>>>>> email me at
>>>>>> sheldon AT slgdev DOT com
>>>>>> I will send it to you. When run, it asks the user whether they
>>>>>> want to open or save the Excel file (so long as they don't have
>>>>>> browser settings set to open in html -- the IE default).
>>>>> Here it is:
>>>>> <?php
>>>>> class ExportToExcel
>>>>> {
>>>>> var $body = '';
>>>>> function setHeader($excel_file_name) {
>>>>> header("Content-type: application/octet-stream");
>>>>> header('Content-Disposition: attachment; filename="' .
>>>>> $excel_file_name . '"');
>>>>> header("Pragma: no-cache");
>>>>> header("Expires: 0");
>>>>> }
>>>>> function export2DArray($arr, $excel_file_name, $title) {
>>>>> $header = "<center><table border=1px>" . $title . "</th>";
>>>>> foreach ($arr as $row) {
>>>>> $body .= "<tr>";
>>>>> foreach ($row as $column) {
>>>>> $body .= "<td>" . $column . "</td>";
>>>>> }
>>>>> $body .= "</tr>"; }
>>>>> $this->setHeader($excel_file_name);
>>>>> echo $header . $body . "</table>";
>>>>> }
>>>>> function addArray($arr) {
>>>>> foreach ($arr as $row) {
>>>>> $this->body .= "<tr>";
>>>>> foreach ($row as $column) {
>>>>> $this->body .= "<td>" . $column . "</td>";
>>>>> }
>>>>> $this->body .= "</tr>"; }
>>>>> }
>>>>> function addTitle($title) {
>>>>> $this->body .= '<center><table border=1px><caption><h4>' .
>>>>> $title .
>>>>> "</h4></caption>";
>>>>> }
>>>>> function addColumnHeading($arr) {
>>>>> $this->body .= '<tr>';
>>>>> foreach ($arr as $column) {
>>>>> $this->body .= '<th>' . $column . "</th>";
>>>>> }
>>>>> $this->body .= "</tr>"; }
>>>>> function closeTable() {
>>>>> $this->body .= "</table>";
>>>>> }
>>>>> function sendArray($excel_file_name) {
>>>>> $this->setHeader($excel_file_name);
>>>>> $this->closeTable();
>>>>> echo $header . $this->body;
>>>>> }
>>>>> function addLines($num) {
>>>>> $this->body .= '<table>';
>>>>> for ($i=0; $i<$num; $i++) {
>>>>> $this->body .= "<tr><td> </td></tr>";
>>>>> }
>>>>> $this->body .= '</table>';
>>>>> }
>>>>> }
>>>>> I use it as follows:
>>>>> $list = 2-d array that I want to write;
>>>>> $excel = new ExportToExcel();
>>>>> $excel->addTitle('Whatever title');
>>>>> $columns = array(comma separated list of headings);
>>>>> $excel->addColumnHeading($columns);
>>>>> $excel->addArray($list);
>>>>> $excel->sendArray(suggested name of the file.xls);
>>>> Sheldon,
>>>> Unfortunately, that doesn't actually create an Excel file - it
>>>> creates an HTML file and calls it an Excel file.
>>>> There isn't anything wrong with that if the client wants to export
>>>> it - but the op indicated his client specifically wants a native
>>>> Excel format program.
>>> While technically true, I suspect what the OP **really** wanted was
>>> for the end user to click a button and have it open up as a file that
>>> can be used in an Excel program and be saved to disk. The actual
>>> coding inside would be immaterial -- just so long as it appears to
>>> the end user as an Excel file and could be opened from his hard drive
>>> using Excel after he saved it there. After all, here is what he wrote:
>>> "I need to get some data from a MySQL database into an Excel
>>> Spreadsheet, potentially using PHP."
>>> What I gave him does exactly that, albeit not in native Excel coding.
>> (in response to Luuk's suggestion to use a csv file)
>> "That was my preferred choice too, but the customer wants it directly
>> in MS-Excel format - they don't want to have to do an import of any
>> sort."
>
> ....and as **I** said, the file opens directly as an Excel file. There
> is no import with my method. To all intents and purposes, it **is** an
> MS-Excel file. It is just that behind the scenes it is not native Excel
> coding. It still is a **real** Excel file (as far as the user is
> concerned). All he has to do if he is using Internet Explorer is to
> make sure his setting is NOT to open an Excel file, ANY Excel file
> native coding or not, as an html file (which is their default setting).
Which completely depends on the client's settings. It may or may not
perform the same as on your system.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
|
|
Posted by NC on November 26, 2008, 9:04 am
Please log in for more thread options
show/hide quoted text
> I need to get some data from a MySQL database into an Excel
> Spreadsheet, potentially using PHP.
Why bother? If you declare your MySQL database as an ODBC source,
you should be able to query it from Excel directly...
Cheers,
NC
|
|
Posted by trookat on November 26, 2008, 7:43 pm
Please log in for more thread options me wrote:
show/hide quoted text
> I need to get some data from a MySQL database into an Excel
> Spreadsheet, potentially using PHP.
>
> What's available in PHP to help me with writing the MS-Excel file?
>
> Thanks,
>
I personally don't have a problem with ANY of the suggests made in these
threads , all valid .
The original poster wants a method to export mysql data to xls format
in a file so his boss can just double click it and it opens no issue.
If you have scoured the internet , googled your brains out regrading xls
and php you have probably discovered that there is no practical solution
show/hide quoted text
to do this in php. I have tried to do php->xls ( pure xls ) stuff
before. There are classes around but none did much more then spew out a
file that then needs to be converted in excel when opened.( causing the
good old conversion window up)
Now the original poster never mentions what version of excel he has to
play with , Excel 2007 uses a new xml type of file format , php is more
equipped to handle that
i would suggest you google :php excel
the first two results
http://www.codeplex.com/PHPExcel http://code.google.com/p/php-excel/
both these projects deal with the xml file version of excel
try these
** DISCLAIMER : I have not tested these so it is unknown if they will
work for you.
If that is not good for you ,then perhaps you need to look at a
different platform.
I hate myself for even thinking this, think about using C# or some-other
Microsoft programming languages they can connect to a mysql db and its
more likely that they will be able to export to .xls natively ( no
guarantee as I'm not a windows programmer)
I suggest if you can find a excel newsgroup/forum they may have a better
solution. I'm sure this problem is common and people have solutions for
you somewhere.
regards trookat
|
|
Posted by r0g on November 27, 2008, 2:11 am
Please log in for more thread options trookat wrote:
show/hide quoted text
> me wrote:
>> I need to get some data from a MySQL database into an Excel
>> Spreadsheet, potentially using PHP.
>> What's available in PHP to help me with writing the MS-Excel file?
>> Thanks,
show/hide quoted text
>
> If that is not good for you ,then perhaps you need to look at a
> different platform.
>
> I hate myself for even thinking this, think about using C# or some-other
> Microsoft programming languages they can connect to a mysql db and its
Yikes, no need to join the dark side! Both Perl and Python are pretty
handy with the ole XLS format too...
http://pypi.python.org/pypi/xlwt
http://homepage.eircom.net/~jmcnamara/perl/WriteExcel.html
There are ways of using both Perl and Python from PHP so you needn't
switch platforms to use them.
Best regards,
Roger.
|
|
Posted by trookat on November 27, 2008, 3:33 am
Please log in for more thread options r0g wrote:
show/hide quoted text
> trookat wrote:
>> me wrote:
>>> I need to get some data from a MySQL database into an Excel
>>> Spreadsheet, potentially using PHP.
>>> What's available in PHP to help me with writing the MS-Excel file?
>>> Thanks,
>
>> If that is not good for you ,then perhaps you need to look at a
>> different platform.
>> I hate myself for even thinking this, think about using C# or some-other
>> Microsoft programming languages they can connect to a mysql db and its
>
>
> Yikes, no need to join the dark side! Both Perl and Python are pretty
> handy with the ole XLS format too...
>
> http://pypi.python.org/pypi/xlwt
>
> http://homepage.eircom.net/~jmcnamara/perl/WriteExcel.html
>
> There are ways of using both Perl and Python from PHP so you needn't
> switch platforms to use them.
>
> Best regards,
>
>
> Roger.
I agree that I may have slightly dipped my toes into the forces of evil
by suggesting a MS product, however one can only recommend what one knows.
To be honest I gave up using perl about when php 3 was released so I
keep forgetting that it may be option. Thats what happens when you get
old and lazy lol
--trookat
( Although ,I'm only 31 so I'm not THAT old, having a disability makes
you age , it also gives me start posting in newsgroups and pretending
I'm doing something useful with my time, like making a wiki system that
I could have just downloaded ..... sorry bout that some monkey must have
got the peanuts out and ran round my head .... I now return you to your
regular television programming.
|
| Similar Threads | Posted | | Excel to MySQL through PHP? | April 1, 2006, 4:27 pm |
| MySQL as Excel? | February 3, 2009, 6:50 pm |
| Export mysql data to excel? | March 10, 2008, 6:03 am |
| Exporting MySQL Data to Excel using PHP | January 5, 2009, 10:18 am |
| Import Excel file into MySQL database | September 3, 2004, 2:27 am |
| Reading Excel to MySql or Comma delimited ... | June 14, 2006, 6:44 pm |
| PHP (mysql) to Excel, Word and PDF and other report formats | May 2, 2009, 4:52 am |
| PHP / MySQL to Excel - good library recommendation? | May 18, 2009, 7:24 am |
| uploading special characters from excel to mysql and php binary/Image | June 17, 2008, 10:47 am |
| PHP COM Excel Obj | May 31, 2006, 7:06 pm |
|
>> sheldonlg wrote:
>>> Jerry Stuckle wrote:
>>>> sheldonlg wrote:
>>>>> sheldonlg wrote:
>>>>>> me wrote:
>>>>>>> I need to get some data from a MySQL database into an Excel
>>>>>>> Spreadsheet, potentially using PHP.
>>>>>>> What's available in PHP to help me with writing the MS-Excel
>>>>>>> file? Thanks,
>>>>>> I have an excel class that I downloaded from somewhere and
>>>>>> modified to my own tastes. It is limited to being able to put up
>>>>>> only one block of data, with a header row and a title row. If you
>>>>>> email me at
>>>>>> sheldon AT slgdev DOT com
>>>>>> I will send it to you. When run, it asks the user whether they
>>>>>> want to open or save the Excel file (so long as they don't have
>>>>>> browser settings set to open in html -- the IE default).
>>>>> Here it is:
>>>>> <?php
>>>>> class ExportToExcel
>>>>> {
>>>>> var $body = '';
>>>>> function setHeader($excel_file_name) {
>>>>> header("Content-type: application/octet-stream");
>>>>> header('Content-Disposition: attachment; filename="' .
>>>>> $excel_file_name . '"');
>>>>> header("Pragma: no-cache");
>>>>> header("Expires: 0");
>>>>> }
>>>>> function export2DArray($arr, $excel_file_name, $title) {
>>>>> $header = "<center><table border=1px>" . $title . "</th>";
>>>>> foreach ($arr as $row) {
>>>>> $body .= "<tr>";
>>>>> foreach ($row as $column) {
>>>>> $body .= "<td>" . $column . "</td>";
>>>>> }
>>>>> $body .= "</tr>"; }
>>>>> $this->setHeader($excel_file_name);
>>>>> echo $header . $body . "</table>";
>>>>> }
>>>>> function addArray($arr) {
>>>>> foreach ($arr as $row) {
>>>>> $this->body .= "<tr>";
>>>>> foreach ($row as $column) {
>>>>> $this->body .= "<td>" . $column . "</td>";
>>>>> }
>>>>> $this->body .= "</tr>"; }
>>>>> }
>>>>> function addTitle($title) {
>>>>> $this->body .= '<center><table border=1px><caption><h4>' .
>>>>> $title .
>>>>> "</h4></caption>";
>>>>> }
>>>>> function addColumnHeading($arr) {
>>>>> $this->body .= '<tr>';
>>>>> foreach ($arr as $column) {
>>>>> $this->body .= '<th>' . $column . "</th>";
>>>>> }
>>>>> $this->body .= "</tr>"; }
>>>>> function closeTable() {
>>>>> $this->body .= "</table>";
>>>>> }
>>>>> function sendArray($excel_file_name) {
>>>>> $this->setHeader($excel_file_name);
>>>>> $this->closeTable();
>>>>> echo $header . $this->body;
>>>>> }
>>>>> function addLines($num) {
>>>>> $this->body .= '<table>';
>>>>> for ($i=0; $i<$num; $i++) {
>>>>> $this->body .= "<tr><td> </td></tr>";
>>>>> }
>>>>> $this->body .= '</table>';
>>>>> }
>>>>> }
>>>>> I use it as follows:
>>>>> $list = 2-d array that I want to write;
>>>>> $excel = new ExportToExcel();
>>>>> $excel->addTitle('Whatever title');
>>>>> $columns = array(comma separated list of headings);
>>>>> $excel->addColumnHeading($columns);
>>>>> $excel->addArray($list);
>>>>> $excel->sendArray(suggested name of the file.xls);
>>>> Sheldon,
>>>> Unfortunately, that doesn't actually create an Excel file - it
>>>> creates an HTML file and calls it an Excel file.
>>>> There isn't anything wrong with that if the client wants to export
>>>> it - but the op indicated his client specifically wants a native
>>>> Excel format program.
>>> While technically true, I suspect what the OP **really** wanted was
>>> for the end user to click a button and have it open up as a file that
>>> can be used in an Excel program and be saved to disk. The actual
>>> coding inside would be immaterial -- just so long as it appears to
>>> the end user as an Excel file and could be opened from his hard drive
>>> using Excel after he saved it there. After all, here is what he wrote:
>>> "I need to get some data from a MySQL database into an Excel
>>> Spreadsheet, potentially using PHP."
>>> What I gave him does exactly that, albeit not in native Excel coding.
>> (in response to Luuk's suggestion to use a csv file)
>> "That was my preferred choice too, but the customer wants it directly
>> in MS-Excel format - they don't want to have to do an import of any
>> sort."
>
> ....and as **I** said, the file opens directly as an Excel file. There
> is no import with my method. To all intents and purposes, it **is** an
> MS-Excel file. It is just that behind the scenes it is not native Excel
> coding. It still is a **real** Excel file (as far as the user is
> concerned). All he has to do if he is using Internet Explorer is to
> make sure his setting is NOT to open an Excel file, ANY Excel file
> native coding or not, as an html file (which is their default setting).