iPixel Posted February 17, 2011 Share Posted February 17, 2011 So i have this script, it pull data via mysql and inserts it into an excel file. However it does not properly tab each field as it should nor go to a new lines as it should instead it stick everything into 1 whole row. Ok but here's the kicker... this same identical script runs 100% fine on another website. The only difference is the query string. There it works fine on this new site it bugs out. any idea where the bug is? <?php include('dbcon.php'); $tablename = $_POST['tablename']; $full_query = "SELECT * FROM $tablename"; $header = ''; $data = ''; $export = mysql_query($full_query); $fields = mysql_num_fields($export); for ($i = 0; $i < $fields; $i++) { $header .= mysql_field_name($export, $i) . "\t"; } while($row = mysql_fetch_row($export)) { $line = ''; foreach($row as $value) { if ((!isset($value)) OR ($value == "")) { $value = "\t"; } else { $value = str_replace('"', '""', $value); $value = '"' . $value . '"' . "\t"; } $line .= $value; } $data .= trim($line)."\n"; } $data = str_replace("\r","",$data); if ($data == "") { $data = "\n(0) Records Found!\n"; } header("Content-type: application/octet-stream"); header("Content-Disposition: attachment; filename=monet_search_results.xls"); header("Pragma: no-cache"); header("Expires: 0"); print "$header\n$data"; ?> PS: This is a linux box if that makes a difference. THANKS! Link to comment https://forums.phpfreaks.com/topic/228009-export-to-excel-issue/ Share on other sites More sharing options...
BlueSkyIS Posted February 17, 2011 Share Posted February 17, 2011 i suspect it's the line-endings. you can try \n and/or \r\n instead. but i really suggest that you try phpexcel. it is a very easy-to-use class that makes real Excel files (not just tab-delimited as in your code). you can add worksheets, cell styles, all kinds of stuff if you need to. http://phpexcel.codeplex.com/ Link to comment https://forums.phpfreaks.com/topic/228009-export-to-excel-issue/#findComment-1175760 Share on other sites More sharing options...
iPixel Posted February 17, 2011 Author Share Posted February 17, 2011 \r\n didnt change anything, and while phpexcel seems great, looking at it just kinda makes me feel it's way to elaborate for what i need. Link to comment https://forums.phpfreaks.com/topic/228009-export-to-excel-issue/#findComment-1175764 Share on other sites More sharing options...
iPixel Posted February 17, 2011 Author Share Posted February 17, 2011 So my workaround, replaced all the \t's with commas and exported as csv instead of xls. and voila . Link to comment https://forums.phpfreaks.com/topic/228009-export-to-excel-issue/#findComment-1175796 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.