-------------- Original message ----------------------
From: Sam Smith <[EMAIL PROTECTED]>
> 
> I've got it working writing out a file (fopen) to CSV (comma delimited) and
> the "header('Content-Disposition: attachment; filename="myFile.csv"')"
> method but it's clumsy for the user to figure out how to use the file.
> 
> Is there some totally slick way of spitting out a file that Excel can open
> right up?

Instead of opening a file and writing to it, just push excel headers and then 
just print like you would nomally do when generating a web page.  If you output 
an html table excel will render it like a spreadsheet.  I don't think I phrased 
that very well, so here's an example
<?
//get your result set up here
if($sExport){
        if ($sExport == 'xls'){
                header("Content-Type: application/vnd.ms-excel");
                header("Content-Disposition: attachment; filename=report.xls");
                $joiner = "</td><td>";
                $begin = "<tr><td>";
                $end = "</td></tr>\n";
                print "<table border='1'>";
        } else{
                header("Content-Type: text/plain");
                header("Content-Disposition: attachment; filename=report.csv");
                $joiner = ",";
                $begin = "";
                $end = "\n";
        }

        print $begin .implode($joiner, $aDisplayColumns).$end;  //prints out a 
header row with column name (stored in $aDisplayColumns with row name as key 
and display value as value)
        while ($row = $dbObj->fetch_array($rs)){
                unset ($push);
                $push = array();
                foreach ($aDisplayColumns as $key => $val)      { 
                        if ($sExport=='xls'){
                                $push[] = $row[$key];
                        }else{
                                $push[] = addslases($row[$key]);
                        }
                }
                print $begin .implode($joiner, $push).$end;
        }
        if ($sExport=='xls'){
                print "</table>";
        }
        exit;
}

Hope that helps and is not too confusing.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to