how do you output result to excel using a button

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jpaloyo
Forum Newbie
Posts: 12
Joined: Tue May 23, 2006 12:53 pm

how do you output result to excel using a button

Post by jpaloyo »

feyd | Please use

Code: Select all

,

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

,

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]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Pear has a library to export data to excel files.
jpaloyo
Forum Newbie
Posts: 12
Joined: Tue May 23, 2006 12:53 pm

Post 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.
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

Post by jmut »

jpaloyo
Forum Newbie
Posts: 12
Joined: Tue May 23, 2006 12:53 pm

Post by jpaloyo »

Thank you so much guys. Sorry for the trouble. I Really appeciate the assistance. :D
jpaloyo
Forum Newbie
Posts: 12
Joined: Tue May 23, 2006 12:53 pm

Post 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

and/or

Code: Select all

tags when [url=http://forums.devnetwork.net/viewtopic.php?t=21171]posting code in the forum[/url].[/size]
User avatar
andym01480
Forum Contributor
Posts: 390
Joined: Wed Apr 19, 2006 5:01 pm

Post 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????
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

Post 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

and/or

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
jpaloyo
Forum Newbie
Posts: 12
Joined: Tue May 23, 2006 12:53 pm

Post by jpaloyo »

feyd | Please use

Code: Select all

,

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

,

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]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

jpaloyo, please start using the syntax higlighting.
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

Post by jmut »

jpaloyo wrote:feyd | Please use

Code: Select all

,

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

,

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.
jpaloyo
Forum Newbie
Posts: 12
Joined: Tue May 23, 2006 12:53 pm

Post by jpaloyo »

thank you i will try. :D
Post Reply