将查询结果导出到Excel CSV时始终显示HTML代码 [英] HTML codes always showing up when I export query result to Excel CSV

查看:126
本文介绍了将查询结果导出到Excel CSV时始终显示HTML代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从SQL Server导出查询结果.我成功完成了一半,因为我有1个问题.页面的HTML代码或界面包含在导出的.csv文件中.如何从导出的文件中删除它?我有下面的代码可以给您一个想法:)

I am exporting the results of a query from SQL Server. I half successful in doing it coz I have 1 problem. The HTML codes or interface of the page gets included in the exported .csv file. How do I remove it from the exported file? I have the codes below to give you an idea :)

另外,如果您还有其他技巧,例如我 希望它采用Excel格式(xlsx),请指导我:)

Also, if you have other tips on how can I improve this thing, like I want it to be in Excel Format (xlsx), please guide me :)

<html>
<head>
<title>Generate Report</title>
<script>
function MessageBox()
{
    alert('Clicked!');
}
</script>
</head>
<body>

<form method='post'>
<input type='date' name='dateStart'>  <input type='date' name='dateEnd'>
<input type='submit' name='btnSubmit' onclick='MessageBox()'> <br>
</form>
</body>
</html>";
<?php

include "db.php";
$con = SetConn();

if(isset($_POST['btnSubmit']))
{
    header("Content-type: text/csv; charset=UTF-8");
    header('Content-Disposition: attachment; filename=Export.csv');
    $start = $_POST['dateStart'];
    $end = $_POST['dateEnd'];
    $user_query = sqlsrv_query($con, "SELECT TOP 100 * FROM [db_nps].[dbo].[survey_report] WHERE (time_stamp >= '$start' and time_stamp <= '$end')");

    //While loop to fetch the records
    $contents = "it_id, it_site, it_oraclenumber, it_lastname, it_firstname, ttsd_number\n";
    while($row = sqlsrv_fetch_array($user_query, SQLSRV_FETCH_ASSOC))
    {
        $contents.=$row['it_id'].",";
        $contents.=$row['it_site'].",";
        $contents.=$row['it_oraclenumber'].",";
        $contents.=$row['it_lastname'].",";
        $contents.=$row['it_firstname'].",";
        $contents.=$row['ttsd_number']."\n";
    }

    $contents_final = chr(255).chr(254).mb_convert_encoding($contents, "UTF-16LE","UTF-8");
    print $contents_final;
}
?>

推荐答案

您正在告诉PHP使用回显的所有内容作为CSV文件的内容.是的,您在回显HTML之后就这样做了,但这没关系;全部都在同一时间处理.

You are telling PHP to use everything echoed as the content for your CSV file. Yes, you are doing so after you echo your HTML, but it doesn't matter; it is all processed at the same time.

使用header()方法时,是在告诉浏览器它接收的内容是某种类型的内容.在您的情况下,您告诉它要接收的内容是应下载的CSV文档.

When you use the header() method, you are telling the browser that the content it is receiving is of a certain type. In your case, you are telling it that the content it is to receive is a CSV document that it should download.

header()放在文件中的位置无关紧要,只要它位于printreadFile语句之前即可.

It does not matter where header() is placed within the file, as long as it is before the print or readFile statements.

因此,基本上,您的浏览器正在执行的操作是读取整个页面,其中包括HTML 作为CSV文档的纯文本.请记住,虽然PHP会按顺序处理自身,但浏览器会同时从服务器接收所有信息,由于您是在告诉它以CSV格式下载文档,所以整个过程都需要页面.

So essentially what your browser is doing is reading the entirety of the page, including the HTML as plain text for a CSV document. Remember that while PHP will process itself in order, the browser receives all of the information from the server at the same time, and since you are telling it to download the document as a CSV, it takes the whole page.

我对您的建议是将打印代码放在单独的外部PHP文档中并链接到该文档,而不是尝试在回显HTML的页面上进行打印.

My advice to you is to put the printing code in a separate external PHP document and link to it, rather than trying to do it on a page that echoes HTML.

这篇关于将查询结果导出到Excel CSV时始终显示HTML代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆