导出/下载表格HTML-PHP到Excel文件 [英] Export / Download Table HTML-PHP to Excel file

查看:75
本文介绍了导出/下载表格HTML-PHP到Excel文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在HTML / PHP上有一张表,用于从mySQL中提取数据。请参见此链接。例如,请搜索此货件代码以获取表格内的结果: 42-7278954

I have a table on HTML/PHP that extract data from mySQL. Please see in this link. For example, please search this shipment code to get the result inside the table: 42-7278954.

之后,我要导出效果出色(您也可以在此处看到按钮)。

After that, I want to export the result to excel (you also can see the button there).

下面是我查看的表代码(我删除了一些列以缩短代码):

Below is my table code in view (I've deleted some columns to shorten the code):

$body = "";

$display =  "<table class='table_searchshipment' id='table_searchshipment' cellpadding='0' cellspacing='0' border='0'>";

            echo $display;
            $body .= $display;

            $display  = "<thead>
                        <tr>
                          <th>CN No.</th>
                          <th>Doc No.</th>
                        </tr>
                      </thead>";
            echo $display;
            $body .= $display;  

            while($row = mysqli_fetch_array($result)) {
                $CN_no= $row['CN_no'];
                $doc_no= $row['doc_no'];

                $display  = "<tr>
                        <td>".$CN_no."</td>
                        <td>".$doc_no."</td>
                      </tr>";
               echo $display;
               $body .= $display;
            }

            $display  = "</table>";
            echo $display;
            $body .= $display;
            $body = htmlspecialchars($body);

            echo '<form action = "setheader" method = "post">
                    <input type = "hidden" name = "body" value = "<?php echo $body ; ?>">
                <input type = "submit" name = "submit" Value = "Export to excel">
            </form>'; 

            if(isset($_POST['submit'])){ 
                    $body = $_POST['body'];
                    setHeader("export.xls");

                    echo $body;
                }

然后在控制器中,我设置函数setheader:

And in the controller, I make function setheader:

public function setheader($excel_file_name)//this function used to set the header variable
{   
    header("Content-type: application/octet-stream");//A MIME attachment with the content type "application/octet-stream" is a binary file.
    //Typically, it will be an application or a document that must be opened in an application, such as a spreadsheet or word processor. 
    header("Content-Disposition: attachment; filename=$excel_file_name");//with this extension of file name you tell what kind of file it is.
    header("Pragma: no-cache");//Prevent Caching
    header("Expires: 0");//Expires and 0 mean that the browser will not cache the page on your hard drive
   }

出现下载弹出窗口,但仅下载 setheader文件包含错误通知。请在此处查看。

The download pop-up is appear, but it only download "setheader" file that contains of error notification. Please see it here.

推荐答案

首先,

此行需要更改为:

echo '<form action = "setheader" method = "post">
        <input type = "hidden" name = "body" value = "<?php echo $body ; ?>">
    <input type = "submit" name = "submit" Value = "Export to excel">
</form>';

至:

echo '<form action = "setheader" method = "post">
        <input type = "hidden" name = "body" value = "'.$body.'">
    <input type = "submit" name = "submit" Value = "Export to excel">
</form>';

第二, $ body = htmlspecialchars($ body); 最有可能返回一个空字符串。

Secondly, $body = htmlspecialchars($body); is most likely returning an empty string.

PHP文档


如果输入字符串在$ b中包含无效的代码单元序列除非设置了
ENT_IGNORE或ENT_SUBSTITUTE标志,否则将返回给定的$ b编码为空字符串。

If the input string contains an invalid code unit sequence within the given encoding an empty string will be returned, unless either the ENT_IGNORE or ENT_SUBSTITUTE flags are set.

但是您可以尝试:

$ body = htmlspecialchars($ body,ENT_QUOTES | ENT_SUBSTITUTE,'UTF-8'); 看看是否能解决该问题。

$body = htmlspecialchars($body, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8'); to see if it solves that issue.

这篇关于导出/下载表格HTML-PHP到Excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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