Welcome to Dream.In.Code
Getting PHP Help is Easy!

Join 99,785 PHP Programmers for FREE! Ask your question and get quick answers from experts. There are 1,569 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



Output data to excel

 
Reply to this topicStart new topic

Output data to excel, Doesn't generate file; just outputs in page

Chupa85
post 7 May, 2008 - 10:03 AM
Post #1


New D.I.C Head

*
Joined: 17 Sep, 2007
Posts: 17


My Contributions


I know I just posted another thread, but this is an entirely different problem. So I am trying to get this code to draw data from the MySQL database and place it into an excel file. The only thing I am getting is the page cluttered with the data. This is not code that I generated; I am just trying to make it work. I've made some modificaitons to the code to suit my current situation but it is not working. As always, any help would be greatly appreciated. Without a doubt the help I have gotten here has helped me greatly this semester, and I thank everyone for it. /Brown nosing.

CODE

<?php
                //Table selection If statement
        if ($_POST['MailingSelection']  == 'Sponsors') {
            $Table = Sponsors;
        }
        else {
            $Table = Members;
            }
            
        include_once("dbconnect.php");

        $result = mysql_query("SELECT * FROM $Table", $con);
        $count = mysql_num_fields($result);

        for ($i = 0; $i < $count; $i++){
            $header .= mysql_field_name($result, $i)."\t";
        }

        while($row = mysql_fetch_row($result)){
             $line = '';
          foreach($row as $value){
            if(!isset($value) || $value == ""){
                  $value = "\t";
        }else{
        # important to escape any quotes to preserve them in the data.
              $value = str_replace('"', '""', $value);
        # needed to encapsulate data in quotes because some data  might be multi line.
        # the good news is that numbers remain numbers in Excel even though quoted.
              $value = '"' . $value . '"' . "\t";
            }
            $line .= $value;
          }
          $data .= trim($line)."\n";
        }
        # this line is needed because returns embedded in the data have "\r"
        # and this looks like a "box character" in Excel
          $data = str_replace("\r", "", $data);
        
        # This line will stream the file to the user rather than spray it across the screen
        header("Content-type: application/vnd.ms-excel");

        # replace excelfile.xls with whatever you want the filename to default to
        header("Content-Disposition: inline; filename=".$_POST['MailingSelection'].".xls");

        echo $header."\n".$data;
        ?>
User is offlineProfile CardPM

Go to the top of the page


Martyr2
post 8 May, 2008 - 02:40 PM
Post #2


Programming Theoretician

Group Icon
Joined: 18 Apr, 2007
Posts: 3,967



Thanked 45 times

Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions


Treat the data as if you were putting it in an HTML table. For instance here I take the id of each member in the regionalstaff table and put it into an excel spreadsheet I call "blah.xls". Notice that I construct a HTML style table and put each item in a <td> tag. What this will do is treat each cell of the table as each cell of the spreadsheet.

CODE

$result = mysql_query("Select * from regionalstaff",$link);
        
$header = "<table border='0' cellpadding='0' cellspacing='0'>";

while ($row = mysql_fetch_array($result)) {
     $header .= "<tr><td>". $row["id"] . "</td></tr>";
}

$header .= "</table>";
        

header("Content-type: application/vnd.ms-excel");
header("Content-Disposition: inline; filename=blah.xls");

echo $header;



So after this is run all the ids will be in column A of my spreadsheet and it will act more like the normal excel you have grown to love. Each value will be in each cell of the spreadsheet.

Hope this is what you were asking about.

Enjoy!

"At DIC we be spreadsheet generating code ninjas... some of the ladies man style ninjas all split the sheets with the ladies" decap.gif
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 7/25/08 01:31AM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month
-->