php - 将 HTML-PHP 表导出/下载到 Excel 文件

标签 php html codeigniter export-to-excel

我有一个 HTML/PHP 表格,可以从 mySQL 中提取数据。请参阅此link 。例如,请搜索此发货代码以获取表中的结果:42-7278954

之后,我想将结果导出到 Excel(您也可以看到那里的按钮)。

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

$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;
                }

在 Controller 中,我创建函数 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”文件。请查看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);很可能返回空字符串。

来自PHP Docs

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'); 看看它是否解决了该问题。

关于php - 将 HTML-PHP 表导出/下载到 Excel 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41356463/

相关文章:

jquery - 如何将 2 个或多个 jquery keydown 函数合并为一个函数?

html - 这是什么数据格式?

php - 在 Codeigniter + PostgreSql 中使用 $this->db->num_rows()

css - 如何避免dompdf中的分页符

javascript - iframe DOM 问题 : undefined values when accessing form field

php - 在并行 plesk 服务器中安装 codeigniter

php - Laravel 5 : Change navbar if user is logged

php - Laravel 在 leftjoin 处分页

php - 登录后如何访问特定的mysql数据行和列?

php - 在angular ngrepeat中使用从服务器返回的数据