Jump to content

Recommended Posts

Hi,

 

I have a database and I need to generate xls file selecting only few columns. The following should happen on click of a trigger like button/link:

 

1) 6 columns from mysql database table are selected. The first row of xls is the field name which is NOT the same as mysql table column name (xls columns are explicitly defined)

2) This file is generated and saved inside filesystem on remote server.

3) Link to this file is displayed which can be downloaded from server.

 

Please help how to do this. I have the code for creating csv files but that doesnt save the file on server, just shoots it to browser to download.

 

function createCSV()
{
        include("dbconnect.php");	
$query = "SELECT `EmailAddress` ,
`Name` ,
`FirstName` ,
`LastName` 

FROM directtable;";
$rsSearchResults = mysql_query($query) or die(mysql_error());
$out = '';
$fields = mysql_list_fields($dbDatabase,'directtable');
$columns = mysql_num_fields($fields);

// Put the name of all fields
for ($i = 0; $i < $columns; $i++) 
{
	$l=mysql_field_name($fields, $i);
	$out .= '"'.$l.'",';
}
$out .="\n";

// Add all values in the table
while ($l = mysql_fetch_array($rsSearchResults)) 
{
	for ($i = 0; $i < $columns; $i++) 
	{
		$out .='"'.$l["$i"].'",';
	}
	$out .="\n";
}
// Output to browser with appropriate mime type, you choose 
header("Content-type: text/x-csv");
//header("Content-type: text/csv");
//header("Content-type: application/csv");
$now=date("d-m-Y,H:i:s", time());
header("Content-Disposition: attachment; filename=emails_".$now.".csv");
echo $out;	

}

Link to comment
https://forums.phpfreaks.com/topic/225627-create-and-store-xls-files/
Share on other sites

Write it to a file instead of ouputting the headers.

function createCSV()
{
$now=date("d-m-Y,H:i:s", time());
$fh=fopen("emails_".$now.".csv","a"); 
include("dbconnect.php");
$query = "SELECT `EmailAddress` ,
`Name` ,
`FirstName` ,
`LastName` 
FROM directtable;";
$rsSearchResults = mysql_query($query) or die(mysql_error());
$fields = mysql_list_fields($dbDatabase,'directtable');
$columns = mysql_num_fields($fields);
// Put the name of all fields
for ($i = 0; $i < $columns; $i++) 
{
$l=mysql_field_name($fields, $i);
fwrite($fh, '"'.$l.'",');
}
fwrite($fh,"\n"); 
// Add all values in the table
while ($l = mysql_fetch_array($rsSearchResults)) 
{
for ($i = 0; $i < $columns; $i++) 
{
fwrite($fh,'"'.$l["$i"].'",');
}
fwrite($fh,"\n");
}
// Output to browser with appropriate mime type, you choose 
//header("Content-type: text/x-csv");
//header("Content-type: text/csv");
//header("Content-type: application/csv");
//$now=date("d-m-Y,H:i:s", time());
//header("Content-Disposition: attachment; filename=emails_".$now.".csv");
//echo $out;
fclose($fh);
}

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.