这篇文章主要介绍了python 数据生成excel导出(xlwt,wlsxwrite)代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
话不多说,看代码:
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 &#