Jump to content

export table to xls (or cvs)?


kemper

Recommended Posts

How can I code to export a table to Excel (or cvs file)?

 

I have my youth sports schedule that I am repeatedly asked to send as Excel file.  I want to have a link for my members to download to Excel (preferred, but can settle for cvs file).

 

Assistance is greatly appreciated.

Link to comment
Share on other sites

For better or worse (and only as an example), here's a script I use. Adjustments should be obvious:

 

<?php
include("support/db-conn.php");
mysql_connect($db_host, $db_login, $db_pass) or die ("Can't connect!"); 
mysql_select_db($db_name) or die ("Can't open database!"); 
$db_table = "dm_scratchpad";

$p="scratchpad_". date("Y-m-d"). ".csv";
$CSV = "Posted_LocalDate,Posted_LocalTime,Message,Importance,Deleted_LocalTime\n"; // headers

$query = "SELECT * FROM $db_table ORDER by posted ASC";
$result = mysql_query($query);
while($myrow = mysql_fetch_array($result)) {
    $CSV.= $myrow[posted]. ",";
    $CSV.= $myrow[timed]. ",";
$message = str_replace("\r\n"," ",$myrow['message']);
$message = str_replace(","," ", $message);
    $CSV.= $message. ",";
    $CSV.= $myrow[importance]. ",";
    $CSV.= $myrow[deleted]. "\n";
}
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"$p\"");
header("Content-Transfer-Encoding: binary");
if(strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE')){
    // IE cannot download from sessions without a cache
    header('Cache-Control: public');
}
echo $CSV;
exit;
?>

Link to comment
Share on other sites

Thanks.

 

I did find a working script while Googling.

 

<?php
$host = 'localhost';
$user = 'mysqlUser';
$pass = 'myUserPass';
$db = 'myDatabase';
$table = 'products_info';
$file = 'export';

$link = mysql_connect($host, $user, $pass) or die("Can not connect." . mysql_error());
mysql_select_db($db) or die("Can not connect.");

$result = mysql_query("SHOW COLUMNS FROM ".$table."");
$i = 0;
if (mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_assoc($result)) {
$csv_output .= $row['Field'].", ";
$i++;
}
}
$csv_output .= "\n";

$values = mysql_query("SELECT * FROM ".$table."");
while ($rowr = mysql_fetch_row($values)) {
for ($j=0;$j<$i;$j++) {
$csv_output .= $rowr[$j].", ";
}
$csv_output .= "\n";
}

$filename = $file."_".date("Y-m-d_H-i",time());
header("Content-type: application/vnd.ms-excel");
header("Content-disposition: csv" . date("Y-m-d") . ".csv");
header( "Content-disposition: filename=".$filename.".csv");
print $csv_output;
exit;
?>

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.