Skip to content

Instantly share code, notes, and snippets.

@taojy123
Created May 18, 2017 06:25
Show Gist options
  • Save taojy123/8d262619104e1c5cbf1dea692a61061c to your computer and use it in GitHub Desktop.
Save taojy123/8d262619104e1c5cbf1dea692a61061c to your computer and use it in GitHub Desktop.
django upload and read xls file by xlrd
def import_data(request):
data = request.FILES.get('data')
book = xlrd.open_workbook(data.name, file_contents=data.read())
sheet = book.sheets()[0]
names = sheet.col_values(1, 1)
stocks = sheet.col_values(5, 1)
dates = sheet.col_values(22, 1)
for name, stock, date in zip(names, stocks, dates):
product, created = Product.objects.get_or_create(name=name)
product.stock = stock
product.date = date
product.save()
return HttpResponse('ok')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment