2017-02-19 93 views
0

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

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

回答

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> 
相关问题