Zergman Posted October 2, 2008 Share Posted October 2, 2008 So I was asked for there to be a button on a page that when clicked, will export the recordset to excel. I can't seem to get this going but this is what I have found so far. 1) Using something like this to load the page right into excel <?php header('Content-Type: application/vnd.ms-excel'); //IE and Opera header('Content-Type: application/x-msexcel'); // Other browsers header('Expires: 0'); header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); ?> 2) Followed this tutorial http://wellknownmeats.com/export-html-data-to-ms-excel-a-phpjavascript-solution/ but can't get it to work either. I don't mind using the header() option but I can't get it to work properly. Don't know how to use it in my page. I keep moving it around, but it just generates errors or does nothing at all. Any help would be greatly appreciated Quote Link to comment https://forums.phpfreaks.com/topic/126825-export-query-to-excel-with-button/ Share on other sites More sharing options...
DeanWhitehouse Posted October 2, 2008 Share Posted October 2, 2008 What are the errors? Quote Link to comment https://forums.phpfreaks.com/topic/126825-export-query-to-excel-with-button/#findComment-655989 Share on other sites More sharing options...
Zergman Posted October 2, 2008 Author Share Posted October 2, 2008 Warning: Cannot modify header information - headers already sent by .... Im guessing its due to my lack of knowledge on how to use header(). I read a bunch from the php manual, but apparently didn't sink in Quote Link to comment https://forums.phpfreaks.com/topic/126825-export-query-to-excel-with-button/#findComment-655990 Share on other sites More sharing options...
DeanWhitehouse Posted October 2, 2008 Share Posted October 2, 2008 Can i assume you didnt read the forum post here at the top about header errors? http://www.phpfreaks.com/forums/index.php/topic,37442.0.html http://www.phpfreaks.com/forums/index.php/board,1.0.html (try looking) Please do But you cannot send a header if there is any HTML outputted before it, or you can cheat and put ob_start(); at the top of the page Quote Link to comment https://forums.phpfreaks.com/topic/126825-export-query-to-excel-with-button/#findComment-655993 Share on other sites More sharing options...
Zergman Posted October 2, 2008 Author Share Posted October 2, 2008 I did read it but it hasn't sunk in yet. I think I just don't understand where to put the header() code in my page. Correct me if im wrong <php code with quieries and stuff> <form> <header()> ...rest of page. ugh, seems so simple lol Quote Link to comment https://forums.phpfreaks.com/topic/126825-export-query-to-excel-with-button/#findComment-655994 Share on other sites More sharing options...
DeanWhitehouse Posted October 2, 2008 Share Posted October 2, 2008 Your wrong, do you know what HTML is, you can't have any HTML at all before the header for example <?php echo "HTML "; header(); ?> will produce error Quote Link to comment https://forums.phpfreaks.com/topic/126825-export-query-to-excel-with-button/#findComment-655995 Share on other sites More sharing options...
Zergman Posted October 2, 2008 Author Share Posted October 2, 2008 I put this at the top of my page but it does nothing. Page just loads normally but no errors. <?php header("Content-Type: application/vnd.ms-excel"); header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Content-Type: application/force-download"); header("Content-Type: application/octet-stream"); header("Content-Type: application/download");; header("Content-Disposition: attachment;filename=NAME_OF_FILE.xls "); ?> Sorry if im frustrating you, this stuff just doesn't make sense to me today Quote Link to comment https://forums.phpfreaks.com/topic/126825-export-query-to-excel-with-button/#findComment-655997 Share on other sites More sharing options...
DeanWhitehouse Posted October 2, 2008 Share Posted October 2, 2008 It should download it straight away, but im not sure about some of them headers Quote Link to comment https://forums.phpfreaks.com/topic/126825-export-query-to-excel-with-button/#findComment-655999 Share on other sites More sharing options...
Zergman Posted October 2, 2008 Author Share Posted October 2, 2008 Could this be blocked by the server or a browser setting? Quote Link to comment https://forums.phpfreaks.com/topic/126825-export-query-to-excel-with-button/#findComment-656001 Share on other sites More sharing options...
DeanWhitehouse Posted October 2, 2008 Share Posted October 2, 2008 try <?php header("Content-Type: application/vnd.ms-excel"); header("Expires: 0");//and this maybe, just guessing, but do trial and error header("Cache-Control: must-revalidate, post-check=0, pre-check=0");//and this header("Content-Type: application/force-download");// remove this header("Content-Type: application/octet-stream"); // try removing this header("Content-Type: application/download"); // you had 2 ; and remove this header("Content-Disposition: attachment;filename=NAME_OF_FILE.xls "); ?> Quote Link to comment https://forums.phpfreaks.com/topic/126825-export-query-to-excel-with-button/#findComment-656002 Share on other sites More sharing options...
Zergman Posted October 2, 2008 Author Share Posted October 2, 2008 Thanks Blade280891, it works in firefox, but no go in IE. Should I assume browser issue then? Quote Link to comment https://forums.phpfreaks.com/topic/126825-export-query-to-excel-with-button/#findComment-656004 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.