zoukankan      html  css  js  c++  java
  • django上传excel文件

    def uploadGrade(request):
        '''
        班级信息导入
        :param request:
        :return:
        '''
        if request.method == 'POST':
            f = request.FILES.get('file')
            excel_type = f.name.split('.')[1]
            if excel_type in ['xlsx','xls']:
                # 开始解析上传的excel表格
                wb = xlrd.open_workbook(filename=None,file_contents=f.read())
                table = wb.sheets()[0]
                rows = table.nrows  # 总行数
                try:
                    with transaction.atomic():  # 控制数据库事务交易
                        for i in range(1,rows):
                            rowVlaues = table.row_values(i)
                            major = models.TMajor.objects.filter(majorid=rowVlaues[1]).first()
                            models.TGrade.objects.create(gradeid=rowVlaues[0],major=major,gradename=rowVlaues[2],memo=rowVlaues[3])
                except:
                    logger.error('解析excel文件或者数据插入错误')
                return render(request,'bg/success.html',{'message':'导入成功'})
            else:
                logger.error('上传文件类型错误!')
                return render(request,'bg/failed.html',{'message':'导入失败'})
    

      

  • 相关阅读:
    迪杰斯特拉(Dijkstra)算法描述及理解
    KMP初步
    网络流初步
    Cutting Codeforces Round #493 (Div. 2)
    优先队列小结
    树状数组初步理解
    分块思想
    树状数组-逆序对-HDU6318
    线段树
    8.12.5
  • 原文地址:https://www.cnblogs.com/majianyu/p/10565227.html
Copyright © 2011-2022 走看看