Jump to content

Export MySQL to Excel OR Creating Excel with PHP


Recommended Posts

I have 2 questions.

 

1. How would I go about exporting my MySQL data into a Microsoft Excel file so the user can download it? For instance, the user goes to /download_db.php. I want the file to run a MySQL query which saves all the information to an excel file and allows the user to download it.

 

2. How would I use PHP to read and write to an excel file? So every time someone enters their email address in a form, the PHP should open /file.xls, read it, append the new email address to the file, and save it. That way the user could just go to /file.xls and read/download the information.

 

Please help on both questions if you can. :) I'm only going to be doing 1 of the 2, whichever is quicker.

 

Thanks,

  Wes

Link to comment
Share on other sites

<?php
	$table = $_POST['table'];
	$selectmain = "SELECT * FROM $table ";
	$result = mysql_query($selectmain) or trigger_error("SQL", E_USER_ERROR);
	$count = mysql_num_fields($result);

	for ($i = 0; $i < $count; $i++){
		$header .= mysql_field_name($result, $i)."\t";
	}

	while($row = mysql_fetch_row($result)){
		$line = '';
		foreach($row as $value){
			if(!isset($value) || $value == ""){
				$value = "\t";
			}else{
				$value = str_replace('"', '', $value); 
				$value = '"\'' . $value . '"' . "\t";
			}
			$line .= $value;
		}
		$data .= trim($line)."\n";
	}
	# this line is needed because returns embedded in the data have "\r"
	# and this looks like a "box character" in Excel
	$data = str_replace("\r", "", $data);

	# Nice to let someone know that the search came up empty.
	# Otherwise only the column name headers will be output to Excel.
	if ($data == "") {
		$data = "\nno matching records found\n";
	}

	# This line will stream the file to the user rather than spray it across the screen
	//header("Content-Type: application/vnd.ms-excel; name='excel'");

	header("Content-type: application/octet-stream");
	header("Content-Disposition: attachment; filename=excelfile.xls");
	header("Pragma: no-cache");
	header("Expires: 0");
	echo $header."\n".$data;

?>

Link to comment
Share on other sites

This fixes the extra '

 

<?php
$table = "mytablename";

$data = "";
$header = "";
$selectmain = "SELECT * FROM $table ";
$result = mysql_query($selectmain) or trigger_error("SQL", E_USER_ERROR);
$count = mysql_num_fields($result);
for ($i = 0; $i < $count; $i++){
$header .= mysql_field_name($result, $i)."\t";
}
while($row = mysql_fetch_row($result)){
$line = '';
foreach($row as $value){
	if(!isset($value) || $value == ""){
		$value = "\t";
	}else{
		$value = str_replace('"', '', $value); 
		$value = $value . "\t";
	}
	$line .= $value;
}
$data .= trim($line)."\n";
}
# this line is needed because returns embedded in the data have "\r"
# and this looks like a "box character" in Excel
$data = str_replace("\r", "", $data);

# Nice to let someone know that the search came up empty.
# Otherwise only the column name headers will be output to Excel.
if ($data == "") {
$data = "\nno matching records found\n";
}
# This line will stream the file to the user rather than spray it across the screen
//header("Content-Type: application/vnd.ms-excel; name='excel'");

header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=excelfile.xls");
header("Pragma: no-cache");
header("Expires: 0");
echo $header."\n".$data;
?>

Link to comment
Share on other sites

I copied the code from a project I did, and there was a reason why it was there, but I forget why. I remember that I was importing/exporting data to/from excel and was diddling with quotation errors - probably had something to do with that.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.