2

我想使用 Codeigniter 将 MYSQL 数据库中的数据导出到 DOC 文件中。我的代码如下:

名为“profile_top_view.php”的视图,其中锚点声明为:

<?php echo anchor('welcome/todoc','Export Posts to DOC File') ?>

名为“welcome.php”的控制器具有以下功能:

 public function todoc() {
    $id = $this->tank_auth->get_user_id();
    $this->mpdf->useOnlyCoreFonts = true;
    $filename = "POSTS";
    $data['member'] = $this->s_model->alldata($id);
    $this->load->view('export_posts_doc_view', $data, true);
    $this->index();
}

一个名为“export_posts_doc_view.php”的视图,其中将为 DOC 文件创建一个表:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>Exported Posts in PDF File</title>
    </head>
    <body>
        <?php
        header("Content-Type: application/vnd.ms-word");
        header("Expires: 0");
        header("Cache-Control:  must-revalidate, post-check=0, pre-check=0");
        header("Content-disposition: attachment; filename=\"posts.doc\"");
        ?>


        <div id="container">
            <h4>Posts</h4>
            <table border="1">
                <tr>
                    <th>title</th>
                    <th>content</th>
                    <th>group</th>
                    <th>video_url</th>
                    <th>pic_path</th>
                    <th>name</th>
                    <th>phone</th>
                    <th>email</th>
                    <th>yes_no</th>
                    <th>single-line-text</th>
                    <th>para_text</th>
                    <th>pdf_file_name</th>
                    <th>add_photo_name</th>
                </tr>
<?php
foreach ($member as $rows) {
    ?>
                    <tr>
                        <td><?php echo $rows['title'] ?></td>
                        <td><?php echo $rows['content'] ?></td>
                        <td><?php echo $rows['group'] ?></td>
                        <td><?php echo $rows['video_url'] ?></td>
                        <td><?php echo $rows['pic_path'] ?></td>
                        <td><?php echo $rows['name'] ?></td>
                        <td><?php echo $rows['phone'] ?></td>
                        <td><?php echo $rows['email'] ?></td>
                        <td><?php echo $rows['yes_no'] ?></td>
                        <td><?php echo $rows['single-line-text'] ?></td>
                        <td><?php echo $rows['para_text'] ?></td>
                        <td><?php echo $rows['pdf_file_name'] ?></td>
                        <td><?php echo $rows['add_photo_name'] ?></td>
                    </tr>
    <?php
}
?>
            </table>

            <br> <br>

        </div>
    </body>
</html>

还有一个名为“s_model.php”的模型,具有以下功能:

  function alldata($id)
    {
        $this->db->select('');
        $this->db->from('posts');
            $this->db->where('user-id',$id);
        $getData = $this->db->get();
        if($getData->num_rows() > 0)
        return $getData->result_array();
        else return null;
    }

如果我将那个标题保留在那里,那么它会显示错误:

没有找到该网址的网页: http ://www.my_ip.com/project/welcome/todoc

但是,如果我删除相同的标题,它会显示控制器中回显的内容。我从以下 URL 获得了该标题: exporting MS word documents using codeigniter?

谁能让我知道我该怎么办?

提前致谢..

4

2 回答 2

2

得到了答案,

我在控制器“welcome.php”中做了一些更改:

public function todoc() {
        $id = $this->tank_auth->get_user_id();
        $this->mpdf->useOnlyCoreFonts = true;
        $filename = "POSTS";
        $data['member'] = $this->s_model->alldata($id);      
        $this->load->view('export_posts_doc_view', $data);
    }
于 2013-03-05T09:30:48.937 回答
0

请参阅下面的代码我希望我会为你工作

    function exportexcel()
    {
    header("Content-type: application/vnd.ms-excel"); 

   /* change the content-type depends upon our requerment */

        header("Content-Disposition: attachment; filename=Sadhak.xls");

        header("Pragma: no-cache");

        header("Expires: 0");

    $table = "your content goes here";

    echo  $table;
    }   
于 2013-10-10T11:55:33.733 回答