i have website created reports. there 3 reports. have button on each page calls php page download results of query fills page csv file. works fine on 1 page, on other 2 gives me error when trying open. says:
the file format , extension of 'filename' don't match. file corrupted or unsafe. unless trust source, don't open it. want open anyway?
i click yes, this:
excel has detected 'filename' sylk file, cannot load it. either file has errors or not sylk file format. click ok try open file in different format.
i click ok , opens fine.
where on other page opens.
here's of exporttoexcel.php
switch ($_post['exporttoexcel']) { case "qdef": $tsql = "select id,qsrc,qname,qdef,isactive,runreport,filepath pmdb.v_qdefs order id"; $hsql = "select headings tableheadings tablename = 'v_qdefs' , headings != 'edit' order id"; break; case "tableupdates": $tsql = "select id, tablename, updatedate pmdb.tableupdates order updatedate"; $hsql = "select headings tableheadings tablename = 'tableupdates' , headings != 'edit' order id"; break; } $filename = $_post['exporttoexcel']; header("content-type: application/x-csv"); header("content-disposition: attachment; filename=$filename.csv"); //define separators (defines columns in excel) $sep = ","; $br = "\r\n"; //line break $getheadings = $conn->query($hsql); $rheadings = $getheadings->fetchall(pdo::fetch_assoc); $headings = array(); $numheadings = count($rheadings); ($i = 0;$i < $numheadings; $i++) { $headings[] = $rheadings[$i]["headings"]; } //start of printing column names names of sql fields foreach($headings $heading => $value) { echo "$value" . $sep; } //end of printing column names echo $br; //separate headers , data foreach($conn->query($tsql) $row) { ($i = 0;$i < $numheadings;$i++) { $commentpos = strpos($rheadings[$i]["headings"],"comment"); $newlines = array("\r\n","\n\r","\n","\r"); $usevalue = str_replace($newlines, " ",$row[$i]); $usevalue = str_replace('"', "'",$row[$i]); $pos = strpos($usevalue,","); if ($commentpos === false || $pos === false || isset($usevalue)) { echo '"' . $usevalue . '"' . $sep; } else { echo $usevalue . $sep . "not quoted"; } } echo $br; }
i have include
@ top has connection string connecting mssql db, work, or there'd nothing displayed on page begin with.
i con't figure out why page doesn't work same way reports when calling page same way.
edit again
i tried several ideas below , have this:
$filename = $_post['exporttoexcel'] . '.csv'; $opened = fopen($filename,'w'); header("content-type: text/csv; charset=utf-8'"); header("content-disposition: attachment; filename=$filename"); fputcsv($opened,$headings); foreach($conn->query($tsql) $row) { fputcsv($opened,$row); } fclose($opened);
which still gives me blank spreadsheet. i'm still doing wrong?
nevermind, updated headings tables trying export first column no longer id
, instead id
. since it's no longer caps working fine without fputcsv
since isn't doing me anyhow.
$filename = $_post['exporttoexcel'] . '.csv'; header("content-type: text/csv; charset=utf-8'"); header("content-disposition: attachment; filename=$filename"); //define separators (defines columns in excel) $sep = ","; $br = "\r\n"; //line break $getheadings = $conn->query($hsql); $rheadings = $getheadings->fetchall(pdo::fetch_assoc); $headings = array(); $numheadings = count($rheadings); ($i = 0;$i < $numheadings; $i++) { $headings[] = $rheadings[$i]["headings"]; } //start of printing column names names of sql fields foreach($headings $heading => $value) { echo "$value" . $sep; } foreach($conn->query($tsql) $row) { ($i = 0;$i < $numheadings;$i++) { $commentpos = strpos($rheadings[$i]["headings"],"comment"); $newlines = array("\r\n","\n\r","\n","\r"); $usevalue = str_replace($newlines, " ",$row[$i]); $usevalue = str_replace('"', "'",$row[$i]); $pos = strpos($usevalue,","); if ($commentpos === false || $pos === false || isset($usevalue)) { echo '"' . $usevalue . '"' . $sep; } else { echo $usevalue . $sep . "not quoted"; } } echo $br; }
Comments
Post a Comment