Jump to content


This topic is now archived and is closed to further replies.


Troubles exporting to excel in IE

Recommended Posts

hey everyone,

I have a php script that dumps an sql query to an excel file for download (based on the tutorial on this site). the code works fine in firefox, downloads as the right file type, name (submission.xls), etc. however, IE downloads the file as an excel file whose name is the name of the script (event_results.php?display=true&rend=xl), which in turn causes excel to generate an error when attempting to open the file.

what could cause this?

thanks very much for the help.

here's the code i'm using:

// Start the session

// Fetch the event's id and render method
$eid = $_GET['eid'];
$renderMethod = $_GET['rend'];

if(!$_SESSION['user'] || (!$_SESSION['isSuper'] && !RegistrationAdmin::checkEventOwner($eid, $_SESSION['userId']))){ //no echo's or header info
    echo "You are not authorized to view this page.";
} else {

    // Fetch the submitted data for this event
    $submissionData = RegistrationAdmin::fetchEventSubmissionData($eid); //no echo's or header info

        //Extract the headings for each column
        $tmpArr = array_values($submissionData);
        $headings = array_keys($tmpArr[0]);
        // Render the results appropriately
            case "xl":
                    // Send the proper headers for an excel download
                    header("Content-type: application/vnd.ms-excel");
                    header("Expires: 0");
                    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
                    header("Content-Disposition: attachment; filename=org_data.xls");
                    //header("Pragma: no-cache");
                    // Render the data in an excel file
                    $renderStr = "";
                    foreach ($headings as $heading){
                        $renderStr .= "\"$heading\"\t";
                    $renderStr .= "\n";
                    foreach ($submissionData as $submission){
                        foreach ($headings as $heading){
                            $renderStr .= "\"".$submission[$heading]."\"\t";
                        $renderStr .= "\n";
            case "txt":
                // Render the data in an HTML table
                $renderStr = "<table cellpadding=\"5\" border>
                foreach ($headings as $heading){
                    $renderStr .= "<th>$heading</th>";
                $renderStr .= "</tr>";
                foreach ($submissionData as $submission){
                    $renderStr .= "<tr>";
                    foreach ($headings as $heading){
                        $renderStr .= "<td align=\"center\">".$submission[$heading]."</td>";
                    $renderStr .= "</tr>";
                $renderStr .= "</table>";
        // Perform the actual rendering
        echo $renderStr;
    } else {
        echo "No submissions exist for this event.";

Share this post

Link to post
Share on other sites


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.