django file upload excel table

views.py

def upload(request):
    '''
    :param request:
    :return: Upload the file excel sheet and parse it
    '''
    if request.method == "POST":
        print("post request")
        myform = FileUploadForm(request.POST, request.FILES)

        #Here you can add a mechanism for filtering excel
        if myform.is_valid():
            # print(myform)
            f = request.FILES['my_file']
            print(f)

            #Start parsing the uploaded excel sheet
            wb = xlrd.open_workbook(filename=None, file_contents=f.read()) # The key point is here
            table = wb.sheets()[0]
            nrows = table.nrows  #行数
            ncole = table.ncols  #列数
            print("row :%s, cole: %s" % (nrows, ncole))

            for i in range(1, nrows):
                rowValues ​​= table.row_values(i) #One row of data

                print(type(rowValues[10]))
                R_projectname=rowValues[1]

                print('rowValues-->{}'.format(R_projectname))

                pf = PhoneMsg.objects.filter(M_name = R_projectname)
                # pf = PhoneMsg.objects.all()
                if not pf.exists():   #空值
                    return render(request,'rc_test/upFileFail.html',context={'error':u'R_projectname does not exist, contact the administrator to add it!'})

                print(pf)

                pm = PhoneMsg.objects.get(M_name =R_projectname)
                pm.save()
                re = Result() #Instantiate the result table
                re.R_projectname = R_projectname
                re.R_name = rowValues[2]
                re.R_version = rowValues[3]
                re.R_context = rowValues[4]
                re.R_result = rowValues[5]
                re.R_note = rowValues[6]
                re.R_ower = rowValues[7]
                re.R_kexuan = rowValues[8]
                re.R_inning = rowValues[9]
                re.R_createtime = datetime(*xldate_as_tuple(rowValues[10],0))
                print(datetime(*xldate_as_tuple(rowValues[10],0)))
                re.save()
                pm.result.add(re)

            handle_upload_file(f, str(f)) #Upload file processing

        return render(request, "rc_test/upFileSuccess.html")


    else:
        print("get request")
        myform = FileUploadForm()
    return render(request, 'rc_test/upFile.html', context={'form': myform, 'what': "file transfer"})

Front-end interface:

    

forms.py

from  django import forms


class FileUploadForm(forms.Form):
    name = forms.CharField(max_length=20,min_length=3,required=True,label='名称:')
    my_file = forms.FileField(label='File name:')

fileup.html

    <div class="container">
        <div class="row">

            <div class="col-md-8 col-md-offset-2">
                <div><h3 style="text-align: center">{{what}}</h3></div>
                <form method="post" enctype="multipart/form-data" action="{% url 'upload' %}"
                      class="table table-bordered form-horizontal">
                    {% csrf_token %}
                    {{ form.as_p }}
                    <input type="submit" value="提交"/>
                </form>

            </div>

        </div>
    </div>

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325537640&siteId=291194637