|
Posted by sheldonlg on November 26, 2008, 7:31 am
Please log in for more thread options
sheldonlg 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,
>
> 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) {
show/hide quoted text
$header = "<center><table border=1px>" . $title . "</th>";
foreach ($arr as $row) {
show/hide quoted text
$body .= "<tr>";
foreach ($row as $column) {
show/hide quoted text
$body .= "<td>" . $column . "</td>";
}
show/hide quoted text
$body .= "</tr>";
}
show/hide quoted text
$this->setHeader($excel_file_name);
echo $header . $body . "</table>";
}
function addArray($arr) {
foreach ($arr as $row) {
show/hide quoted text
$this->body .= "<tr>";
foreach ($row as $column) {
show/hide quoted text
$this->body .= "<td>" . $column . "</td>";
}
show/hide quoted text
$this->body .= "</tr>";
}
}
function addTitle($title) {
show/hide quoted text
$this->body .= '<center><table border=1px><caption><h4>' . $title .
"</h4></caption>";
}
function addColumnHeading($arr) {
show/hide quoted text
$this->body .= '<tr>';
foreach ($arr as $column) {
show/hide quoted text
$this->body .= '<th>' . $column . "</th>";
}
show/hide quoted text
$this->body .= "</tr>";
}
function closeTable() {
show/hide quoted text
$this->body .= "</table>";
}
function sendArray($excel_file_name) {
show/hide quoted text
$this->setHeader($excel_file_name);
$this->closeTable();
echo $header . $this->body;
}
function addLines($num) {
show/hide quoted text
$this->body .= '<table>';
for ($i=0; $i<$num; $i++) {
show/hide quoted text
$this->body .= "<tr><td> </td></tr>";
}
show/hide quoted text
$this->body .= '</table>';
}
}
show/hide quoted text
?>
I use it as follows:
$list = 2-d array that I want to write;
$excel = new ExportToExcel();
show/hide quoted text
$excel->addTitle('Whatever title');
$columns = array(comma separated list of headings);
show/hide quoted text
$excel->addColumnHeading($columns);
$excel->addArray($list);
$excel->sendArray(suggested name of the file.xls);
|
|
Posted by Jerry Stuckle on November 26, 2008, 2:21 pm
Please log in for more thread options
sheldonlg wrote:
show/hide quoted text
> 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.
I suspect it could be done with a COM object, but I've never done it
myself - just always wrote a csv file.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
|
|
Posted by sheldonlg on November 26, 2008, 3:35 pm
Please log in for more thread options Jerry Stuckle wrote:
show/hide quoted text
> 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.
|
|
Posted by Jerry Stuckle on November 26, 2008, 4:03 pm
Please log in for more thread options sheldonlg wrote:
show/hide quoted text
> 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."
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
|
|
Posted by sheldonlg on November 26, 2008, 7:47 pm
Please log in for more thread options Jerry Stuckle wrote:
show/hide quoted text
> 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).
|
| 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 |
|
>> 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).