Cannot create file to download

Topics: Developer Forum, User Forum
Mar 8, 2010 at 5:46 PM

Hello everyone,

I'm trying to export the content of a MySQL table to a xls file. This works fine using XAMPP for testing, but when I uploaded the script containing the code to my webspace and tried to run it, it just opened an empty excel file, although the table was correctly exported locally on my computer (XAMPP).

This function contains the code

function excel()
{
	global $data, $tabl;
	global $show, $order;
	
	$filename = $data . '_' . $tabl . '_' . date('Y-m-d-H-i-s') . '.xls';
	header('Content-Type: application/vnd.ms-excel');
	header('Content-Disposition: attachment;filename="' . $filename . '"');
	header('Cache-Control: max-age=0');

	require_once('PHPExcel 1.7.2/Classes/PHPExcel.php');
	require_once('PHPExcel 1.7.2/Classes/PHPExcel/IOFactory.php');
	
	$objPHPExcel = new PHPExcel();	//new PHPExcel object
		
	$ci = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q');	//col index
	$ri = 1;	//row index
	$cc = 0;	//set col counter
	
	$qry	= "SELECT ";
	if($_POST['show_ln'])	{ $qry .= "ln, ";	$objPHPExcel->getActiveSheet()->setCellValue($ci[$cc] . '1', 'Zeile');		$cc++; }
	if($_POST['show_datum']){ $qry .= "datum, ";    $objPHPExcel->getActiveSheet()->setCellValue($ci[$cc] . '1', 'Datum');		$cc++; }
	if($_POST['show_bad'])	{ $qry .= "bad, ";	$objPHPExcel->getActiveSheet()->setCellValue($ci[$cc] . '1', 'Bad');		$cc++; }
	if($_POST['show_zeit'])	{ $qry .= "zeit, ";	$objPHPExcel->getActiveSheet()->setCellValue($ci[$cc] . '1', 'Uhrzeit');	$cc++; }
	if($_POST['show_kurs'])	{ $qry .= "kurs, ";	$objPHPExcel->getActiveSheet()->setCellValue($ci[$cc] . '1', 'Kurs');		$cc++; }
	if($_POST['show_nn'])	{ $qry .= "nn, ";	$objPHPExcel->getActiveSheet()->setCellValue($ci[$cc] . '1', 'Nachname');	$cc++; }
	if($_POST['show_vn'])	{ $qry .= "vn, ";	$objPHPExcel->getActiveSheet()->setCellValue($ci[$cc] . '1', 'Vorname');	$cc++; }
	if($_POST['show_geb'])	{ $qry .= "geb, ";	$objPHPExcel->getActiveSheet()->setCellValue($ci[$cc] . '1', 'Geburtstag');	$cc++; }
	if($_POST['show_plz'])	{ $qry .= "plz, ";	$objPHPExcel->getActiveSheet()->setCellValue($ci[$cc] . '1', 'PLZ');		$cc++; }
	if($_POST['show_ort'])	{ $qry .= "ort, ";	$objPHPExcel->getActiveSheet()->setCellValue($ci[$cc] . '1', 'Wohnort');	$cc++; }
	if($_POST['show_tel'])	{ $qry .= "tel, ";	$objPHPExcel->getActiveSheet()->setCellValue($ci[$cc] . '1', 'Tel.nummer');	$cc++; }
	if($_POST['show_email']){ $qry .= "email, ";    $objPHPExcel->getActiveSheet()->setCellValue($ci[$cc] . '1', 'E-Mail');		$cc++; }
	if($_POST['show_anm'])	{ $qry .= "anm, ";	$objPHPExcel->getActiveSheet()->setCellValue($ci[$cc] . '1', 'Anmerkung');	$cc++; }
	if($_POST['show_start']){ $qry .= "start, ";    $objPHPExcel->getActiveSheet()->setCellValue($ci[$cc] . '1', 'Start');		$cc++; }
	if($_POST['show_an'])	{ $qry .= "an, ";	$objPHPExcel->getActiveSheet()->setCellValue($ci[$cc] . '1', 'Anschrift');	$cc++; }
	if($_POST['show_zahl'])	{ $qry .= "zahl, ";	$objPHPExcel->getActiveSheet()->setCellValue($ci[$cc] . '1', 'Zahlung');	$cc++; }
	if($_POST['show_bez'])	{ $qry .= "bez, ";	$objPHPExcel->getActiveSheet()->setCellValue($ci[$cc] . '1', 'bezahlt');	$cc++; }
	$qry = substr($qry, 0, -2);	//cut off last comma
	$qry	.= " FROM " . $tabl . $order;

	$res = mysql_query($qry) or die(mysql_error());
	
	while($row = mysql_fetch_row($res))
	{
		$ri++;		//increase row index
		$cc = 0;	//set col counter
		foreach($row as $val)
		{			
			$objPHPExcel->getActiveSheet()->setCellValue($ci[$cc] . $ri, $val);
			
			$cc++;	//increase col counter
		}
	}
		
	$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
	$objWriter->save('php://output');

	exit();
}

I don't know  why this works well offline but doesn't do it online.

Thanks for your help and best wishes,

  Fredl

Mar 11, 2010 at 8:52 PM

Check if you have php-xml. I've got the same problem and after lot of debugging - I found that ExcelWriter crashes while creating new XMLWriter(). After installing php-xml - everything works OK for me.

Mar 12, 2010 at 10:15 AM

Thanks for your help. However, I found the error in the meantime.