Created
May 18, 2017 06:25
-
-
Save taojy123/8d262619104e1c5cbf1dea692a61061c to your computer and use it in GitHub Desktop.
django upload and read xls file by xlrd
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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