Codeigniter - 以DOC格式导出数据库数据

时间:2013-03-05 07:01:09

标签: codeigniter ms-word doc

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

查看名为'profile_top_view.php',其中anchor声明为:

<?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

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

任何人都可以告诉我该怎么办?

提前致谢..

2 个答案:

答案 0 :(得分: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);
    }

答案 1 :(得分: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;
    }