CSV下载不适用于所有页面


CSV Download not working for all pages

我有一个为报表创建的网站。基本上有3份报告。我在每个页面上都有一个按钮,它调用另一个PHP页面,该页面将下载将页面填充到CSV文件中的查询结果。这在一个页面上运行良好,但在另两个页面上,打开时会出现错误。上面写着:

"FileName"的文件格式和扩展名不匹配。文件可能已损坏或不安全。除非你信任它的来源,否则不要打开它。你想打开它吗?

我点击是,然后得到这个:

Excel检测到"FileName"是SYLK文件,但无法加载它。该文件有错误或不是SYLK格式的文件。单击"确定"尝试以其他格式打开文件。

我单击确定,它会很好地打开。

就像在另一页上一样,它只是打开。

以下是ExportToExcel.php 的大部分内容

switch ($_POST['ExportToExcel'])
{
    case "QDef":
        $tsql = "select Id,QSrc,QName,QDef,isActive,RunReport,FilePath from pmdb.v_QDefs order by Id";
        $hsql = "select Headings from TableHeadings where TableName = 'v_QDefs' and Headings != 'Edit' order by ID";
        break;
    case "TableUpdates":
        $tsql = "select ID, TableName, UpdateDate from pmdb.TableUpdates order by UpdateDate";
        $hsql = "select Headings from TableHeadings where TableName = 'TableUpdates' and Headings != 'Edit' order by 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);
for ($i = 0;$i < $NumHeadings; $i++)
{
    $headings[] = $rHeadings[$i]["Headings"];
}
//start of printing column names as names of SQL fields
foreach($headings as $Heading => $value)
{
    echo "$value" . $sep;
}
//end of printing column names
echo $br; //separate the headers and the data
foreach($conn->query($tsql) as $row)
{
    for ($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;
}

我在顶部有一个include,它有用于连接到我的MSSQL数据库的连接字符串,这确实有效,否则页面上一开始就不会显示任何内容。

我只是不明白为什么当所有报告都以相同的方式调用页面时,页面对所有报告的工作方式都不一样。

再次编辑

我尝试了下面的几个想法,现在我有了这个:

$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) as $row)
{
    fputcsv($Opened,$row);
}
fclose($Opened);

仍然给了我一个空白的电子表格。很明显我还是做错了什么?

Nevermind,我更新了要导出的所有表的Headings,使第一列不再是ID,而是Id。由于它不再是全大写的,所以没有fputcsv也能很好地工作,因为这对我没有任何帮助。

$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);
for ($i = 0;$i < $NumHeadings; $i++)
{
    $headings[] = $rHeadings[$i]["Headings"];
}
//start of printing column names as names of SQL fields
foreach($headings as $Heading => $value)
{
    echo "$value" . $sep;
}
foreach($conn->query($tsql) as $row)
{
    for ($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;
}