如何在laravel中的数据库中保存word文件中的数据

时间:2017-02-19 20:02:41

标签: laravel-5.2

我想创建一个word文件作为报告。我想从表中获取所有数据并放入word文件,但我无法将数据保存到文件中。我在laravel 5.2做。文件正在创建和下载,但来自数据库的数据未被保存。感谢大家 这是我的控制器代码 -

public function downloadAbleFile(Request $request){
    //Fetching all records based on projectid
    $project_id = $request->input('project_id');
    $questiondetails = DB::table('questionnair')->where('project_id','=',$project_id)->get();

    $headers = array(
        "Content-type"=>"text/html",
        "Content-Disposition"=>"attachment;Filename=report.doc"
    );

    $content = '<html>
                <head>
                <meta charset="utf-8">
                </head>
                <body>
                    <p>
                        <table>
                        <thead>
                        <tr>
                        <th>Project ID</th>
                        <th>Question Number</th>
                        <th>User ID</th>
                        <th>Answer</th>
                        <th>Status</th>
                        </tr>
                        </thead>
                        <tbody>
                        <?php
                            function project(){
                            foreach ($questiondetails as $question){
                                return(
                                    echo "<tr>";
                                    echo "<td>".$question->project_id."</td>";
                                    echo "<td>".$question->question_num."</td>";
                                    echo "<td>".$question->user_id."</td>";
                                    echo "<td>".$question->answer."</td>";
                                    echo "<td>".$question->status."</td>";
                                    echo "</tr>";
                                );
                            }
                        }
                        project();
                        ?>
                        </tbody>
                        </table>
                    </p>
                </body>
                </html>';

    return Response::make($content,200, $headers);
}

1 个答案:

答案 0 :(得分:0)

为什么不使用刀片? 试试这个

在您的控制器中:

public function downloadAbleFile(Request $request){
    $project_id      = $request->input('project_id');
    $questiondetails = DB::table('questionnair')->where('project_id','=',$project_id)->get();

    $headers = array(
        "Content-type"        => "text/html",
        "Content-Disposition" => "attachment;Filename=report.doc"
    );

    $content =  View::make('path.view', [
                    'questiondetails' => $questiondetails,
                ])->render();

    return Response::make($content,200, $headers);
}
在您的视图中

<html>
    <head>
        <meta charset="utf-8">
    </head>
    <body>
        <br>
        <table>
            <thead>
                <tr>
                    <th>Project ID</th>
                    <th>Question Number</th>
                    <th>User ID</th>
                    <th>Answer</th>
                    <th>Status</th>
                </tr>
            </thead>
            <tbody>
                @foreach($questiondetails as $question)
                <tr>
                    <td>{{ $question->project_id }}</td>
                    <td>{{ $question->question_num }}</td>
                    <td>{{ $question->user_id }}</td>
                    <td>{{ $question->answer }}</td>
                    <td>{{ $question->status }}</td>
                </tr>
                @endforeach
            </tbody>
        </table>
        <br>
    </body>
</html>