writing to .csv file to be opened by Excel
Posted: Mon Dec 08, 2008 9:51 am
I want to write to a CSV file and then let user click on link to load Excel.
Is there something special that I have to add to file header to prompt the browser to use Excel to open file.
Now it opens with browser as a text file.
Is there something special that I have to add to file header to prompt the browser to use Excel to open file.
Now it opens with browser as a text file.
Code: Select all
$string = " SELECT * FROM " . $InvoiceHeader ;
$result = fnConnectSQL($string);
$csvLine = "";
// make a unqiue directory name for each customer
$directoryToPutFile = 'D' . $User_id ."_". (md5($User_id)) ;
// if directory does not exist then create it
if (!is_dir($directoryToPutFile))
{
mkdir($directoryToPutFile, 0700);
}
// directory + file name
$myFile = $directoryToPutFile . "/" . $User_id .".csv";
//write to file over-ride existing file
$fh = fopen($myFile, 'w') or die("can't open file");
// create some headers & write to file with fputcsv
$csvLine = array("header1","header2","header3","header4");
fputcsv($fh,$csvLine);
while($row = fnFetchArraySQL($result))
{
$csvLine[0] = $row[0] ;
$csvLine[1] = $row[1] ;
$csvLine[2] = $row[2] ;
$csvLine[3] = $row[3] ;
fputcsv($fh, $csvLine);
}
fclose($fh);
// show icon with link to file
echo " <a href=" .$dq . $myFile . $dq."><img src='excel_icon.jpg' alt='download CSV file'></a>";
?>