Kế hoạch của tôi là cho phép người dùng tải lên tệp excel, sau khi tải lên tôi sẽ hiển thị biểu mẫu có thể chỉnh sửa chứa nội dung của excel được tải lên, khi người dùng xác nhận đầu vào là chính xác, anh/cô nhấn nút lưu và các mục này được lưu ở một số kiểu máy.Django và xlrd, đọc từ bộ nhớ
Đối với điều này, tôi đã viết quan điểm này và hình thức:
dạng:
IMPORT_FILE_TYPES = ['.xls', ]
class XlsInputForm(forms.Form):
input_excel = forms.FileField(required= True, label= u"Upload the Excel file to import to the system.")
def clean_input_excel(self):
input_excel = self.cleaned_data['input_excel']
extension = os.path.splitext(input_excel.name)[1]
if not (extension in IMPORT_FILE_TYPES):
raise forms.ValidationError(u'%s is not a valid excel file. Please make sure your input file is an excel file (Excel 2007 is NOT supported.' % extension)
else:
return input_excel
xem:
def import_excel_view(request):
if request.method == 'POST':
form = XlsInputForm(request.POST, request.FILES)
if form.is_valid():
input_excel = request.FILES['input_excel']
# I need to open this input_excel with input_excel.open_workbook()
return render_to_response('import_excel.html', {'rows': rows})
else:
form = XlsInputForm()
return render_to_response('import_excel.html', {'form': form})
Như bạn có thể nhìn thấy ở # I need to open this input_excel with input_excel.open_workbook()
tôi cần để đọc từ bộ nhớ nhưng open_workbook
đọc từ một tệp mà không lưu đầu vào của một nơi nào đó, làm thế nào tôi có thể đọc nó?
làm việc tuyệt vời, cảm ơn! – Hellnar
Đối với các tệp unicode bạn có thể sử dụng 'book = open_workbook (file_contents = input_excel.read(), encoding_override = 'utf8')' –