Пример #1
0
 def action_import(self, request):
     from django.db import transaction
     if self.attach:
         if self.handler:
             klass = ExcelManager().handlers.get(self.handler)
             with transaction.atomic():
                 klass.handle(self, self.attach)
                 self.status = 1
                 self.save()
         else:
             import xlrd
             import os
             from mis import settings
             path = os.path.join(settings.MEDIA_ROOT, self.attach.name)
             workbook = xlrd.open_workbook(path)
             sheet = workbook.sheet_by_index(0)
             row_count = sheet.nrows
             col_count = sheet.ncols
             cols = []
             with transaction.atomic():
                 for row_index in range(row_count):
                     line = sheet.row_values(row_index)
                     if row_index == 0:
                         cols = line
                         continue
                     elif row_index == 1:
                         continue
                     else:
                         klass = self.content_type.model_class()
                         values = line
                         params = {}
                         for name in cols:
                             index = cols.index(name)
                             v = values[index]
                             if type(v) == str:
                                 v = force_text(v.decode('gbk'))
                             params[name] = v
                             # print( 'name is %s value is %s'%(name,v)
                         try:
                             params.pop('')
                         except Exception as e:
                             pass
                         # print( params
                         klass.objects.create(**params)
                 self.status = '1'
                 self.save()
Пример #2
0
 def action_import(self,request):
     from django.db import transaction
     if self.attach:
         if self.handler:
             klass = ExcelManager().handlers.get(self.handler)
             with transaction.atomic():
                 klass.handle(self,self.attach)
                 self.status = 1
                 self.save()
         else:
             import xlrd
             import os
             from mis import settings
             path = os.path.join(settings.MEDIA_ROOT,self.attach.name)
             workbook = xlrd.open_workbook(path)
             sheet = workbook.sheet_by_index(0)
             row_count = sheet.nrows
             col_count = sheet.ncols
             cols = []
             with transaction.atomic():
                 for row_index in range(row_count):
                     line = sheet.row_values(row_index)
                     if row_index == 0:
                         cols = line
                         continue
                     elif row_index == 1:
                         continue
                     else:
                         klass = self.content_type.model_class()
                         values = line
                         params = {}
                         for name in cols:
                             index = cols.index(name)
                             v = values[index]
                             if type(v) == str:
                                 v = force_text(v.decode('gbk'))
                             params[name]=v
                             # print('name is %s value is %s'%(name,v))
                         try:
                             params.pop('')
                         except Exception as e:
                             pass
                         # print(params)
                         klass.objects.create(**params)
                 self.status = '1'
                 self.save()