我想創建一個出口在我的Django以Excel功能如下:日期時間問題xlsxwriter
def export_myreport(request, sd, ed):
from xlsxwriter.workbook import Workbook
import cStringIO as StringIO
from django.utils.encoding import smart_str
# create a workbook in memory
output = StringIO.StringIO()
wb = Workbook(output)
bg = wb.add_format({'bg_color': '#9CB640', 'font_color': 'black'})
bg2 = wb.add_format({'bg_color': '#FFFFFF', 'font_color': 'black'})
ws = wb.add_worksheet('My Report')
row_num = 0
summary = MyModel.objects.filter(time__range = (sd, ed)).select_related()
row_num += 2
row = [
smart_str(u"Time"),
smart_str(u"Item"),
smart_str(u"User")
]
for col_num in xrange(len(row)):
ws.write(row_num, col_num, row[col_num], bg)
for s in summary:
row_num += 1
row2 = [
s.time,
s.model_name,
s.user.first_name
]
for col_num in xrange(len(row2)):
ws.write(row_num, col_num, row2[col_num], bg2)
wb.close()
output.seek(0)
response = HttpResponse(output.read(), content_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
response['Content-Disposition'] = "attachment; filename=myreport.xlsx"
return response
但我正在逐漸與日期時間格式的一些問題!也許我在這裏想念的東西?
以下是錯誤我得到:
TypeError at /myapp/export_myreport/2015-05-01/2015-05-19
can't subtract offset-naive and offset-aware datetimes
編輯:
這就是我如何調用我的HTML鏈接:
<a href="export_myreport/{{begindate}}/{{enddate}}" class="btn btn-default pull-right" role="button">Export to XLSX</a>
這裏{{begindate}}
和{{enddate}}
屬於角變量。
我發送他們在我的網址 – Abhishek
請發佈完整的回溯。 – alecxe
完成!發佈追蹤 – Abhishek