ajax - 在 laravel 中使用 ajax 下载 maatwebsite excel

标签 ajax excel laravel-5.4 maatwebsite-excel

我正在尝试在 laravel 中使用 ajax 方法下载一个 excel 文件。
Controller 功能:

$myFile = Excel::create($name, function ($excel) use ($export) {
            $excel->sheet('Data', function ($sheet) use ($export) {
                $sheet->fromArray($export);

                $sheet->cells('A1:N1', function ($cells) {

                    $cells->setBackground('#dbdbdb');
                    $cells->setFontColor('#000000');
                    $cells->setFontWeight('bold');
                    $cells->setFont(array(
                        'family' => 'Calibri',
                        'size'   => '9',

                    ));

                });

                $sheet->setStyle(array(
                    'font' => array(
                        'name' => 'Calibri',
                        'size' => 9,

                    ),
                ));

            });
        });
        $myFile   = $myFile->string('xlsx'); 
        $response = array(
            'name' => $name, 
            'file' => "data:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;base64," . base64_encode($myFile), 
        );

        return response()->json($response);

Ajax 功能:
$(document).on('click', '.ExportJobs', function() {
    $.ajaxSetup({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        }
    });
    var ids = [];
    $(".InvoiceCheckBox:checked").each(function(e) {
        ids.push(this.value);
    });
    data = {
        "ids": ids,
    };
    $.ajax({
        method: "POST",
        url: "/exportNew",
        data: data,
        success: function(response) {
            var a = document.createElement("a");
            a.href = response.file;
            a.download = response.name;
            document.body.appendChild(a);
            a.click();
            a.remove();
        }
    });
});

但是,如果我从 xlsx 更改字符串值,则使用上述 Controller 方法不会返回 excel 格式的文件至csv然后 csv正在下载格式化的文件。

我们如何使下载的excel格式文件?任何建议,请!

最佳答案

我知道这已经很晚了,但是为其他像我一样遇到同样问题的人发帖

我还需要使用 ajax post call 从 Maatwebsite excel 库中下载 excel。

  • 添加了一个按钮来触发 ajax 调用以下载 excel 文件
     <button onclick="downloadExcel()" id="btn-download-payroll" class="btn btn-dark-success btn-md" style="transform: translateY(50%); top: 50%; font-size: 13px;"><i aria-hidden="true" class="fa fa-cog mr-10"></i>
                            Download
                        </button>
    

  • 使用以下 js 代码发布 ajax 请求
    function downloadExcel() {
    var salaryMonth = $("#dp-salary-month").datepicker("getDate");
    var department = $("#cbox-department");
    var month = new Date(salaryMonth).getMonth() + 1;
    var year = new Date(salaryMonth).getFullYear();
    $.ajax({
        xhrFields: {
            responseType: 'blob',
        },
        type: 'POST',
        url: '/downloadPayroll',
        data: {
            salaryMonth: month,
            salaryYear: year,
            is_employee_salary: 1,
            department: department.val()
        },
        success: function(result, status, xhr) {
    
            var disposition = xhr.getResponseHeader('content-disposition');
            var matches = /"([^"]*)"/.exec(disposition);
            var filename = (matches != null && matches[1] ? matches[1] : 'salary.xlsx');
    
            // The actual download
            var blob = new Blob([result], {
                type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
            });
            var link = document.createElement('a');
            link.href = window.URL.createObjectURL(blob);
            link.download = filename;
    
            document.body.appendChild(link);
    
            link.click();
            document.body.removeChild(link);
        }
    });
    }
    

    路线/web.php 文件为我的 Controller 设置路由
    Route::post('/downloadPayroll', 'Payroll\\Process\\PayrollController@downloadPayroll');
    

    这里我用了 maat网站/excel 使用 生成 excel 文件的库发件人查询 方法,但由于库更新 Excel::create 已被 中的 Excel::download 替换“maatwebsite/excel”:“^3.1”我在我的情况下使用了下载方法,这是我的 HelperClass 根据我的要求生成记录

    PayrollHelper.php
    namespace App\Http\Helpers;
    
    use App\PayrollEmployee;
    use Maatwebsite\Excel\Concerns\FromQuery;
    use Maatwebsite\Excel\Concerns\Exportable;
    
    class PayrollHelper implements FromQuery
    {
    use Exportable;
    
    public function forDepartment(int $department)
    {
        $this->department = $department;
        return $this;
    }
    
    public function forMonth(string $month)
    {
        $this->month = $month;
        return $this;
    }
    
    public function query()
    {
    // get the salary information for the given month and given department 
        return PayrollEmployee::query()->where(['salary_month' => $this->month,'department_id'=>$this->department]); 
    }
    }
    

    终于在我的 Controller 中
    class PayrollController extends Controller
    {
    public function downloadPayroll(Request $request)
    {
        $file_name = '';
    
    
        try {
            $requestData = $request->all();
            $salary_month = $requestData['salaryMonth'];
            $salary_year = $requestData['salaryYear'];
            $department = $requestData['department'];
            $is_employee_salary = boolval($requestData['is_employee_salary']);
            $month = Carbon::createFromDate($salary_year, $salary_month);
            $month_start = Carbon::parse($month)->startOfMonth();
            $formated_month = Carbon::parse($month)->format('F Y');
            $file_name = 'Employee_salary_' . $formated_month . '.xlsx';
    
            // to download directly need to return file
            return Excel::download((new PayrollHelper)->forMonth($month_start)->forDepartment($department), $file_name, null, [\Maatwebsite\Excel\Excel::XLSX]);
    
    
        } catch (exception $e) {
    
        }
    }
    }
    

    创建excel文件后返回文件获取为ajax 以 Blob 形式响应

    就这样

    关于ajax - 在 laravel 中使用 ajax 下载 maatwebsite excel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46298489/

    相关文章:

    php - Laravel excel,水平阅读

    javascript - 通过ajax Nodejs Express上传带有表单的文件

    javascript - header 失败的 AJAX 请求

    javascript - 如何使用 Jquery 的 AJAX 提交复杂的表单?

    excel - 从 VBA 中的 LongPtr 获取 UDT

    php - 显示用户详细信息及其在 laravel 中的平均评分

    php - Ajax登录问题

    excel - 从 "time"单元格中删除 AM/PM

    excel - getelementsbyclassname Excel vba - 重复调用错误

    Laravel 查询构建器增量更新