Django Excel

def excel_ajax(request):
response = HttpResponse()
response['Content-Disposition'] = 'attachment;filename=export_agencycustomer.xls'
wb = xlwt.Workbook(encoding='utf-8')
sheet = wb.add_sheet(u'订单')
my_time = []
guang = []
for i in SensorData.objects.filter(collertor_id=2):
my_time.extend([i.sensor_time])
guang.extend([i.sensor_guang])
# 处理对应数据获取json格式
data = [1, 2, 3, 4, 5, 6, 7]
List = {'my_time': my_time,
'guang': 2}
# 1st line
sheet.write(0, 0, '光照')
sheet.write(0, 1, '时间')
sheet.write(0, 2, '温度')
sheet.write(0, 3, '湿度')
List = []
row = 1
for i in guang:
sheet.write(row, 0, i) # 此处是在第一列添加数据
# sheet.write(row,1, '测试1')#此处在第二列添加数据
# sheet.write(row,2,'测试2')#此处在第二列添加数据
# sheet.write(row,3,'测试3')#此处在第二列添加数据
row = row + 1
output = StringIO.StringIO()
wb.save(output)
output.seek(0)
response.write(output.getvalue())
return response

posted @ 2015-10-30 00:25  gopher-lin  阅读(229)  评论(0编辑  收藏  举报