heffym Posted February 19, 2009 Share Posted February 19, 2009 When I try and run my script to go into an excel file, instead of inputing the data into my excel file I just get the PHP code within the excel file. Any help would be awesome Link to comment https://forums.phpfreaks.com/topic/145958-trying-to-export-to-excel-and-data-is-not-going/ Share on other sites More sharing options...
MatthewJ Posted February 19, 2009 Share Posted February 19, 2009 Actually seeing the code would be awesome Link to comment https://forums.phpfreaks.com/topic/145958-trying-to-export-to-excel-and-data-is-not-going/#findComment-766254 Share on other sites More sharing options...
heffym Posted February 19, 2009 Author Share Posted February 19, 2009 Is the code in my Excel file <?php // initialize page ID and path variables $thispage = "00-05-00"; require_once("../../includes/pageload.php"); $user_names = GetAllUserNames(); header("Content-Type:application/vnd.ms-excel"); header('Content-Disposition:attachment;filename="report.xls"'); header("Content-Transfer-Encoding:ascii"); header('Expires: 0'); header('Pragma: cache'); header('Cache-Control: private'); $delim = "\t"; require_once(EVROOT . 'includes/accounting/report.inc'); ?> / validate report_id $report_id = intval($_GET['report_id']); if ($report_id <= 0) { header('Location:reports_manager.php'); } // get report $GLOBALS["db"]->SelectDB($_SESSION["db_name"]); $report_info = $GLOBALS["db"]->ReadOneRow("SELECT report_name, function FROM reports WHERE report_id = '" . $report_id . "'"); $report_name =& $report_info['report_name']; $report_func =& $report_info['function']; if (isset($_POST["download_report"])) { $date_range_floor_m = intval($_POST['date_range_floor_m']); $date_range_floor_d = intval($_POST['date_range_floor_d']); $date_range_floor_y = intval($_POST['date_range_floor_y']); $date_range_ceil_m = intval($_POST['date_range_ceil_m']); $date_range_ceil_d = intval($_POST['date_range_ceil_d']); $date_range_ceil_y = intval($_POST['date_range_ceil_y']); $format = ($_POST['format'] == '.csv' || $_POST['format'] == '.txt' ? $_POST['format'] : '.xls'); $date_field = mysql_real_escape_string(strip_tags($_POST['date_field'])); $inclusive = (isset($_POST['inclusive'])); } else { $date_range_floor_m = date("n"); $date_range_floor_d = date("j"); $date_range_floor_y = date("Y"); $date_range_ceil_m = date("n"); $date_range_ceil_d = date("j"); $date_range_ceil_y = date("Y"); $date_field = 'event_date'; $format = '.xls'; if ($report_name == 'Daily Payments Made') { $date_field = 'payment_date'; } $inclusive = true; } // create form fields $df = new Dropdown; $df->name = "date_field"; $df->css_class = "dropdown"; $df->options[] = new Option("event_date", "Event date", $date_field == "event_date"); $df->options[] = new Option("record_created", "Event record created date", $date_field == "record_created"); $df->options[] = new Option("record_last_modified", "Event record last modified date", $date_field == "record_last_modified"); $df->options[] = new Option("payment_date", "Payment date", $date_field == "payment_date"); $rf = new DateSelector("date_range_floor", $date_range_floor_m, $date_range_floor_d, $date_range_floor_y); $rf->start_year = date("Y")-5; $rf->num_years = 10; $rf->css_class = "dropdown"; $rc = new DateSelector("date_range_ceil", $date_range_ceil_m, $date_range_ceil_d, $date_range_ceil_y); //$rc->start_year = date("Y")-5; $rc->num_years = 10; $rc->css_class = "dropdown"; $in = new ChecklistItem; $in->name = "inclusive"; $in->checked = $inclusive; $fo = new Dropdown; $fo->name = "format"; $fo->css_class = "dropdown"; $fo->options[] = new Option(".xls", "Microsoft Excel spreadsheet (.xls)", $format == ".xls"); $fo->options[] = new Option(".csv", "Comma-separated values (.csv)", $format == ".csv"); $fo->options[] = new Option(".txt", "Tab-delimited text (.txt)", $format == ".txt"); if (isset($_POST["download_report"])) { if (!$rf->valid || !$rc->valid) { $onload = "alert('ERROR! One or more of the dates you entered isn\'t valid. Please try again.')"; } else { // generate report header('Location:report' . $format . '?report_id=' . $report_id . '&date_field=' . $date_field . '&date_floor=' . urlencode($rf->value) . '&date_ceil=' . urlencode($rc->value) . '&inclusive=' . intval($inclusive)); session_write_close(); exit; $now = date('Y-m-d-H-i-s'); require_once(EVROOT . 'includes/accounting/' . $report_func . ".php"); echo $report_func; $finaldata = $report_func($date_field, $rf->value, $rc->value, $inclusive); if (sizeof($finaldata) > 0) { switch($format) { case ".txt": $textfile->delimiter = "\t"; $textfile->text_qualifier = false; $textfile->content_type = "text/plain"; $textfile->transfer_encoding = "ascii"; break; case ".csv": $textfile->delimiter = ","; $textfile->text_qualifier = "\""; $textfile->content_type = "text/plain"; $textfile->transfer_encoding = "ascii"; break; case ".xls": default: $textfile->delimiter = "\t"; $textfile->text_qualifier = false; $textfile->content_type = "application/vnd.ms-excel"; $textfile->transfer_encoding = "ascii"; } if ($textfile->CreateDataTextFile(0644)) { session_write_close(); header('Location:' . DOCROOT . 'file_download.php?file=' . $reportfile . '&type=' . $textfile->content_type . '&enc=' . $textfile->transfer_encoding); exit; } else { exit('ERROR: Failed to create downloadable report file. Please notify the administrator.'); } } else { $onload = "alert('NO RESULTS! This report produces no data. Please define a new date field and/or date range, or edit the report.');"; } } } function PrepForCreateTable($f) { return $f . " text"; } Link to comment https://forums.phpfreaks.com/topic/145958-trying-to-export-to-excel-and-data-is-not-going/#findComment-766258 Share on other sites More sharing options...
Mark Baker Posted February 20, 2009 Share Posted February 20, 2009 Is this the code that's being used to generate the file, or the content of the file once it's been generated? It's almost impossible to follow, and you're going to confuse the he** out of any browser that receives all these conflicting headers, and you're not even generating a real Excel file, and with all these echos after the filetype headers you're going to get a very confused file. However, I suspect that somewhere you're reading the actual text of a php script (rather than the file you're meant to be displaying) and dumping that to your output. Link to comment https://forums.phpfreaks.com/topic/145958-trying-to-export-to-excel-and-data-is-not-going/#findComment-767136 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.