#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2008
    Posts
    3
    Rep Power
    0

    Render and display spreadsheet in browser


    Experts ! Please help me achieve my task.
    I need to renter and display spreadsheet in browser using php and shouldn't open the pop up window "Save As" asking user to open/save.It should open the spredsheet in the browser with out tool bar of spredsheet etc .
    the following code works fine and doesn't open pop up window in IE 7.0.
    It shows pop up window in IE 6.0 .Even if i click open ., it opens the spreadsheet in Microsoft Excel env and not in the browser.Please help me fix this issue.


    $export_file = "test.xls";
    //ob_end_clean();
    // ini_set('zlib.output_compression','Off');

    header('Pragma: public');
    header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
    header('Last-Modified: '.gmdate('D, d M Y H:i:s') . ' GMT');
    header('Cache-Control: no-store, no-cache, must-revalidate'); // HTTP/1.1
    header('Cache-Control: pre-check=0, post-check=0, max-age=0'); // HTTP/1.1
    header ("Pragma: no-cache");
    header("Expires: 0");
    header('Content-Transfer-Encoding: none');
    header('Content-Type: application/vnd.ms-excel'); // This should work for IE & Opera
    header("Content-type: application/octet-stream");
    header('Content-Disposition: inline; filename="'.basename($export_file).'"');
    readfile($export_file);
  2. #2
  3. Web Developer/Musician
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Nov 2004
    Location
    Tennessee Mountains
    Posts
    2,408
    Rep Power
    1031
    Try the following code.
    PHP Code:
        function getMimeType($file)
        {
            
    $nameArr explode('.',$file);
            
    $ext $nameArr[(count($nameArr) - 1)];
            switch(
    strtolower($ext))
            {
                case 
    'pdf':
                    
    $type 'application/pdf';
                    break;
                case 
    'jpg':
                    
    $type 'image/jpeg';
                    break;
                case 
    'jpeg':
                    
    $type 'image/jpeg';
                    break;
                case 
    'gif':
                    
    $type 'image/gif';
                    break;
                case 
    'mp4':
                    
    $type 'application/mpeg4-generic';
                    break;
                case 
    'mp3':
                    
    $type 'audio/mpeg';
                    break;
                case 
    'tiff':
                    
    $type 'image/tiff';
                    break;
                case 
    'png':
                    
    $type 'image/png';
                    break;
                case 
    'csv':
                    
    $type 'text/csv';
                    break;
                case 
    'txt':
                    
    $type 'text/plain';
                    break;
                case 
    'xml':
                    
    $type 'text/xml';
                    break;
                case 
    'html':
                    
    $type 'text/html';
                    break;
                case 
    'doc':
                    
    $type 'application/msword';
                    break;
                case 
    'zip':
                    
    $type 'application/zip';
                    break;
                case 
    'jpeg2000':
                    
    $type 'image/jp2';
                    break;
                case 
    'rtf':
                    
    $type 'text/rtf';
                    break;
                case 
    'mov':
                    
    $type 'video/quicktime';
                    break;
                case 
    'xls':
                    
    $type 'application/excel';
                    break;
                case 
    'ppt':
                    
    $type 'application/powerpoint';
                    break;
                case 
    'wav':
                    
    $type 'audio/x-wav';
                    break;
                case 
    'aiff':
                    
    $type 'audio/aiff';
                    break;
                case 
    'mid':
                    
    $type 'audio/x-midi';
                    break;
                case 
    'bmp':
                    
    $type 'image/bmp';
                    break;
                case 
    'mpeg':
                    
    $type 'video/mpeg';
                    break;
                case 
    'mpg':
                    
    $type 'video/mpeg';
                    break;
                case 
    'avi':
                    
    $type 'x-msvideo';
                    break;
                case 
    'pub':
                    
    $type 'application/x-mspublisher';
                    break;
                case 
    'sit':
                    
    $type 'application/x-stuffit';
                    break;
                case 
    'tar':
                    
    $type 'application/x-tar';
                    break;
                case 
    'ra':
                    
    $type 'audio/x-pn-realaudio';
                    break;
                case 
    'ram':
                    
    $type 'audio/x-pn-realaudio';
                    break;
                case 
    'tsv':
                    
    $type 'text/tab-separated-values';
                    break;
                case 
    'asf':
                    
    $type 'video/x-ms-asf';
                    break;
                case 
    'wma':
                    
    $type 'audio/x-ms-wma';
                    break;
                case 
    'wmv':
                    
    $type 'video/x-ms-wmv';
                    break;
                case 
    'flv':
                    
    $type 'video/x-flv';
                    break;
                case 
    'swf':
                    
    $type 'application/x-shockwave-flash';
                    break;
                case 
    'fla':
                    
    $type 'application/octet-stream';
                    break;
                default:
                    
    $type 'application/octet-stream';
                    break;
            }
            return 
    $type;
        }
        
        function 
    downloadHeaders($file,$filelen,$mode='I')
        {
            
    // $mode: D = force download, I = attempt inline
            
    $type getMimeType($file);
            
    $mode strtoupper($mode);
            if(isset(
    $_SESSION))
            {
                
    session_cache_limiter('no-cache');
            }
            
    header('pragma: cache');
            
    header('pragma: public');
            
    header('Cache-Control: max-age=0');
            
    header("Cache-Control: private",false); // required for certain browsers 
            
    header('Content-Type: '.$type);
            
    header("Expires: 0");
            if(
    $mode == 'D')
            {
                
    header('Content-Disposition: attachment; filename='.$file);
            }
            else
            {
                
    header('Content-Disposition: inline; filename='.$file);
            }
            
    header("Content-Transfer-Encoding: binary");
            
    header('Content-Length: '.$filelen);
        }

    // use in this way for inline
    $filename 'test.xls';
    $len filesize($filename);
    downloadHeaders($filename,$len);
    readfile($filename); 
    Keep in mind however, this requires the proper support for Excel within that install of IE, and frankly the way IE handles files, especially for use by plugins or Active X, it's a wonder it works at all. You also won't get spreadsheets to work in any other browsers.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2008
    Posts
    3
    Rep Power
    0
    thanks for the reply. I tried the code your posted.
    1)IE 7.0 observation :
    It doesn't open the pop up windows "save as" but the code opens the spreadsheet in microsoft excel enviornment not in the browser.

    2)IE 6.0 observation :
    The code opens the spreadsheet directly in browser not in the microsoft excel env.

    thanks
  6. #4
  7. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,317
    Rep Power
    7170
    I can't really help you at all on this one, I would simply advise you to try something completely different. What you're trying to do sounds like a compatibility nightmare.
  8. #5
  9. Web Developer/Musician
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Nov 2004
    Location
    Tennessee Mountains
    Posts
    2,408
    Rep Power
    1031
    Originally Posted by sharugan
    thanks for the reply. I tried the code your posted.
    1)IE 7.0 observation :
    It doesn't open the pop up windows "save as" but the code opens the spreadsheet in microsoft excel enviornment not in the browser.

    2)IE 6.0 observation :
    The code opens the spreadsheet directly in browser not in the microsoft excel env.

    thanks
    That's our "good friend" IE being a PITA. Not much you can do about it. Google a little to see if there is a workaround, but obviously MS has just thrown you a curve ball. They love doing that. Maybe there is a third party Active X control that will do it in 7.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2008
    Posts
    3
    Rep Power
    0

    frameset doesn't open excel sheet


    Thakns for the message.I am facing an issue while accessing the excel in the frameset that calls the php code that has a funtionality to show the excel sheet.
    Here is the Scenario
    1)When i directly access the URL of the PHP script that has a logic with inline command to open the excel on browser, it opens the excel in browser or it shows the "save as" dialog based on the "xls file setting" in explorer
    2)When i call the PHP script in a frameset to open the spreadsheet on frame , it doesn't open the "save as" dialog box at all and show empty web page.

    Why doesn't the excel open in a frameset with php call ?

    Expert help me pls!
    <HTML>
    <HEAD>
    <TITLE>Frames</TITLE>
    </HEAD>

    <FRAMESET ROWS="5%, 93%" BORDER="0" FRAMESPACING="0" MARGINHEIGHT="0" MARGINWIDTH="0" >
    <FRAME SRC="content.html" BORDER="0" FRAMESPACING="0" MARGINHEIGHT="0" MARGINWIDTH="0" FRAMEBORDER="0" SCROLLING="no">
    <FRAME SRC="xls.php" BORDER="0" FRAMESPACING="0" MARGINHEIGHT="0" MARGINWIDTH="0" FRAMEBORDER="0" SCROLLING="no">
    <NOFRAMES>
    <P>Here is the <A href="main-noframes.html">
    non-frame based version of the document.</A>
    </NOFRAMES>

    </FRAMESET>

    </HTML>

IMN logo majestic logo threadwatch logo seochat tools logo