Morning,
I have been asked by the finance department to make there reports pulled from the database dump into an excel file. I was able to do that successfully using the PEAR spreadsheet writer, then I realized that there wasn't a way to have the users save to a local file, so I tried an excel class but IE doesn't recognize the file extension and keeps trying to save the PHP file.
I'm sure it has something to do with the headers, but I cant make the bloody thing work.
Any thoughts would be appreciated.
Laura
include('connect.php');
$result = mssql_query("select * from dbo.vwAcctIssue where department ='mil'");
$count = mssql_num_fields($result);
for ($i = 0; $i < $count; $i++){
$header .= mssql_field_name($result, $i)."\t";
}
while($row = mssql_fetch_row($result)){
$line = '';
foreach($row as $value){
if(!isset($value) || $value == ""){
$value = "\t";
}else{
# important to escape any quotes to preserve them in the data.
$value = str_replace('"', '""', $value);
# needed to encapsulate data in quotes because some data might be multi line.
# the good news is that numbers remain numbers in Excel even though quoted.
$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/octet-stream");
header("Content-Disposition: attachment; filename=example.xls");
header("Pragma: no-cache");
header("Expires: 0");
echo $header."\n".$data;