douchuitang0331 2015-08-31 12:12
浏览 54
已采纳

从laravel excel数组创建数据表

i need to upload excel file and create datatable from array. How to do that? I have problem with making postExcel return.

im using http://www.maatwebsite.nl/laravel-excel/docs and http://datatables.net/

Here is my code:

public function postExcel(Request $request)
{
    if($request->ajax()) {

        if ($request->hasFile('file'))
        {
            $file = $request->file('file');
            $filename = $file->getClientOriginalName();

            //$file->getRealPath();
            $path = $file->move(public_path(), $filename);

            \Excel::load($path, function($reader){

                $results = $reader->toArray();

            });
            print_r($results);die();
            $json = array(
                'status' => 'ok',
                'array' => $results
            );
        }
        return Response::json($json);
    }
}

and this is views js there i need generate table:

$(document).on("change", "#file-input", function() {
    var formData = new FormData($('form.upload-excel')[0]);

    $.ajax({
        type: 'POST',
        data: formData,
        url: '/Excel/public/upload-excel',
        cache: false,
        contentType: false,
        processData: false,
        success: function(data){
            if(data.status === 'ok') {
                console.log(data.array);
                $('#table').DataTable({
                    colReorder: true,
                });
            }
        }
    });
});
  • 写回答

1条回答 默认 最新

  • douji9518 2015-08-31 13:38
    关注

    problem solved. it was:

                $data = \Excel::load($path)->toArray();
    
                $json = array(
                    'status' => 'ok',
                    'data' => $data
                );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀