Exporting To Excel - Almost There
Posted: Fri Apr 27, 2007 4:58 am
Hi All,
I have copied the export mysql to excel tutorial from this website and it works fine.
I am trying to ammend the program so that the open,save or cancel box does not popup and the file is just saved to a specific location with a certain name.
ie
C:\temp\export.xls
The reason I need the file to be specific is becuase I want to run macros on the data to create charts and tables etc. I am then dragging the data in the named spreadsheet into a sheet that contains all of the macros. Therefore it is essential that the file is saved in the exact location and not named by the user which could lead to errors.
Here is the code for the export
Many Thanks in Advance
Jamie
I have copied the export mysql to excel tutorial from this website and it works fine.
I am trying to ammend the program so that the open,save or cancel box does not popup and the file is just saved to a specific location with a certain name.
ie
C:\temp\export.xls
The reason I need the file to be specific is becuase I want to run macros on the data to create charts and tables etc. I am then dragging the data in the named spreadsheet into a sheet that contains all of the macros. Therefore it is essential that the file is saved in the exact location and not named by the user which could lead to errors.
Here is the code for the export
Code: Select all
<?php
define (db_host,"mysql.xcalibre.co.uk");
define (db_user,"*****");
define (db_pass,"*****");
define (db_link,mysql_connect(db_host,db_user,db_pass));
define (db_name,"bridgeit");
mysql_select_db(db_name);
$select = "select * from tblResultsDesc";
$export= mysql_query($select);
$fields = @mysql_num_fields($export);
for ($i = 0;$i<$fields;$i++){
$header .= mysql_field_name($export,$i) . "\t";
}
while ($row = @mysql_fetch_row($export)){
$line = '';
foreach($row as $value){
if ((!isset($value))or($value=="")){
$value = "t";
}
else
{
$value = str_replace('"','""',$value);
//echo $value;
$value = '"' . $value . '"' . "\t";
}
$line .=$value;
}
$data .=trim($line)."\n";
//echo $data;
}
$data = str_replace("\r","",$data);
if ($data ==""){
$data = "n(0) Records Found!n";
}
header("Content-Type: application/vnd.ms-excel");
header ("Content-Disposition: attachment;fielname=extraction.xls");
header("pragma:no-cache");
header("Expires:0");
print "$header$data";
ob_end_flush();
?>Many Thanks in Advance
Jamie