python实现数据导出到excel的示例


当前第2页 返回上一页

编写py文件

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

from xlwt import *

import StringIO

from apps.song.models import Song

def excel_ktvsong(request):?

  """?导出excel表格?"""?

  _id = request.GET.get('id', 0)?

  list_obj = Song.objects.filter(is_delete__exact=False)? # django orm 

  if list_obj:? # 创建工作薄?

    ws = Workbook(encoding='utf-8')?

    w = ws.add_sheet(u"歌曲列表")?

    w.write(0, 0, u"歌曲名称")?

    w.write(0, 1, u"歌手")?

    # 写入数据?

    excel_row = 1?

    for obj in list_obj:?

      data_song = obj.song?

      data_singer_name = obj.singer_name?

      w.write(excel_row, 0, data_song)?

      w.write(excel_row, 1, data_singer_name)??

      excel_row += 1??

    sio = StringIO.StringIO()?

    ws.save(sio)?

    sio.seek(0)?

    response = HttpResponse(sio.getvalue(),    

    content_type='application/vnd.ms-excel')?

    response['Content-Disposition'] = 'attachment;filename=%s.xls' % time.strftime('%Y%m%d%H%M%S')?

    response.write(sio.getvalue())?

    return response?

  else:?

    return HttpResponse("无数据")

相关推荐:

使用python实现数据分析

Python实现数据库编程方法详解

以上就是python实现数据导出到excel的示例的详细内容,更多文章请关注木庄网络博客!!

返回前面的内容

相关阅读 >>

怎么用Python画花朵

Python函数之classmethod()

Python怎么导入wmi

Python怎么结束循环

Python如何将字符串等长分割

如何用Python代码温度转换?

Python中怎么运行shell脚本

Python操作mysql代码总结

golang和Python有什么区别?

Python文件读写保存操作的实现代码

更多相关阅读请进入《Python》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...