Page 1 of 1
how do you output result to excel using a button
Posted: Tue May 23, 2006 1:11 pm
by jpaloyo
feyd | Please use Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
I am a beginner and i am not very good at this. can someone please tell how to output php result to excel. here is my code. this works and shows result, i just dont have the button and the code to output the result to excel. Help please
Code: Select all
$query = "select project, track_index, track_name from track_list";
$result = mysql_query($query);
if (mysql_num_rows($result) > 0)
{
echo '<table width=100% cellpadding="5" cellspacing="10" border ="1"';
echo '<tr><td><b>Project</b></td>
<td><b>Index</b></td>
<td><b>Name</b></td></tr>';
while($row = mysql_fetch_row($result))
{
echo '<tr>';
echo '<td>' . $row[0] . '</td>';
echo '<td>' . $row[1] . '</td>';
echo '<td>' . $row[2] . '</td>';
echo '</tr>';
}
echo '</table>';
}
else
{
echo 'no rows found!';
}
mysql_free_result($result);
mysql_close($connect);
?>
feyd | Please use Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Posted: Tue May 23, 2006 2:18 pm
by feyd
Pear has a library to export data to excel files.
Posted: Tue May 23, 2006 3:09 pm
by jpaloyo
i checked pear, but could not find the answer here is something i found using google.
$filename = "some_crap.xls";
//take care of all the excel initialization
$header_temp = "Content-Disposition: attachment; filename=$filename";
header("Content-type: application/octet-stream");
header($header_temp);
header("Pragma: no-cache");
header("Expires: 0");
it said to paste this to my code but i'm not really sure how to use this and im also not sure how to code the button to make it export the files to excel. can someone please show me.
Posted: Wed May 24, 2006 2:52 am
by jmut
Posted: Wed May 24, 2006 1:14 pm
by jpaloyo
Thank you so much guys. Sorry for the trouble. I Really appeciate the assistance.

Posted: Wed May 24, 2006 4:08 pm
by jpaloyo
ugh...i just dont get this stuff. i dont know how to use this to make it work for my code. sorry guys PHP is really hard for me to learn. You guys have probably given me the answer i needed to know but im just not getting it. do i need more than 1 page in order to make this work???
this is the code from pear: this is suppose to export my results to excel.
Code: Select all
<?php
require_once 'Spreadsheet/Excel/Writer.php';
// Creating a workbook
$workbook = new Spreadsheet_Excel_Writer();
// sending HTTP headers
$workbook->send('test.xls');
// Creating a worksheet
$worksheet =& $workbook->addWorksheet('My first worksheet');
// The actual data
$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);
// Let's send the file
$workbook->close();
?>
Burrito: Please use Code: Select all
tags when [url=http://forums.devnetwork.net/viewtopic.php?t=21171]posting code in the forum[/url].[/size]
Posted: Wed May 24, 2006 5:02 pm
by andym01480
What error do you get?
I don't have pear installed on my home server, so the require once would fail..
Folks how do you test PEAR is installed????
Posted: Thu May 25, 2006 1:19 am
by jmut
jpaloyo wrote:ugh...i just dont get this stuff. i dont know how to use this to make it work for my code. sorry guys PHP is really hard for me to learn. You guys have probably given me the answer i needed to know but im just not getting it. do i need more than 1 page in order to make this work???
this is the code from pear: this is suppose to export my results to excel.
Code: Select all
<?php
require_once 'Spreadsheet/Excel/Writer.php';
// Creating a workbook
$workbook = new Spreadsheet_Excel_Writer();
// sending HTTP headers
$workbook->send('test.xls');
// Creating a worksheet
$worksheet =& $workbook->addWorksheet('My first worksheet');
// The actual data
$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);
// Let's send the file
$workbook->close();
?>
Burrito: Please use Code: Select all
tags when [url=http://forums.devnetwork.net/viewtopic.php?t=21171]posting code in the forum[/url].[/size][/quote]
This snippet works perfect for me. It shows me the saveAs dialog in the browser.
Using FF 1.5.0.1
PHP 5.1.2 I am sure it works with >=4.3.8 as well
Posted: Thu May 25, 2006 11:23 am
by jpaloyo
feyd | Please use Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
weird this does not work for me. it show a white page. I double checked to make the tag in all closed and i made sure of the semi colons but its all there. I am using PHP designer 2005 and mysql database. will this code not work for me? this block of code is php from what i can tell. The "require_once" line, is that a class? and if so whats the code behind it to make this block of code work?
Code: Select all
<?php
require_once 'Spreadsheet/Excel/Writer.php';
// Creating a workbook
$workbook = new Spreadsheet_Excel_Writer();
// sending HTTP headers
$workbook->send('test.xls');
// Creating a worksheet
$worksheet =& $workbook->addWorksheet('My first worksheet');
// The actual data
$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);
// Let's send the file
$workbook->close();
?>
feyd | Please use Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Posted: Thu May 25, 2006 3:21 pm
by feyd
jpaloyo, please start using the syntax higlighting.
Posted: Fri May 26, 2006 12:04 am
by jmut
jpaloyo wrote:feyd | Please use Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
weird this does not work for me. it show a white page. I double checked to make the tag in all closed and i made sure of the semi colons but its all there. I am using PHP designer 2005 and mysql database. will this code not work for me? this block of code is php from what i can tell. The "require_once" line, is that a class? and if so whats the code behind it to make this block of code work?
Code: Select all
<?php
require_once 'Spreadsheet/Excel/Writer.php';
// Creating a workbook
$workbook = new Spreadsheet_Excel_Writer();
// sending HTTP headers
$workbook->send('test.xls');
// Creating a worksheet
$worksheet =& $workbook->addWorksheet('My first worksheet');
// The actual data
$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);
// Let's send the file
$workbook->close();
?>
feyd | Please use Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color][/quote]
By your answer I understand you have no idea what PEAR is (although you said you "checked" it).
You need [url=http://pear.php.net/package/Spreadsheet_Excel_Writer]this[/url] package installed in your [url=http://pear.php.net]PEAR[/url]
Make sure it is in your include path. It should be if installed properly (another topic).
Then turn on error_reporting(E_ALL), display_errors and copy/paste the code snippet - simplify the problem.
And if you have problems then...have us know.
Happy coding.
Posted: Fri May 26, 2006 8:57 am
by jpaloyo
thank you i will try.
