Django后端利用xlrd模块处理前端传回的表格

前端html代码:




前端js代码:

					...
					var form = new FormData();
                    var file = $("#file")[0].files[0];
                    form.append('people', file);
					$.ajax({
                            url: '....',
                            data: form,
                            type: "POST",
                            dataType: 'json',
                            processData: false,
                            contentType: false,
                            success: function (result) {
                               ...
                        });
                        ...


后端代码:

import xlrd


@csrf_exempt
def people_file(request):
    # 前端传回表格
    upload_file = request.FILES.get('people')
    # 读取表格文件
    wb = xlrd.open_workbook(filename=None, file_contents=upload_file.read())
    # 获取相应的表格,如果一个表格文件中包含多个sheet,可以用下标区分
    table = wb.sheets()[0]
    # 获取表格行数
    rows = table.nrows
    try:
        for i in range(1, rows):
            # 获取每一行,数组形式保存每一列的值
            column = table.row_values(i)
            for co in column:
                print(co)
        return JsonResponse({"data": 200})
    except Exception as e:
        print('出错:%s' % e.args)
        return JsonResponse({"data": 400})

你可能感兴趣的:(python)