国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > Python > 正文

python實(shí)現(xiàn)數(shù)據(jù)導(dǎo)出到excel的示例--普通格式

2020-02-22 23:58:57
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

此文是在django框架下編寫(xiě),從數(shù)據(jù)庫(kù)中獲取數(shù)據(jù)使用的是django-orm

用python導(dǎo)出數(shù)據(jù)到excel,簡(jiǎn)單到爆!(普通的excel格式)

安裝xlwt

pip install xlwt

編寫(xiě)py文件

from xlwt import *import StringIOfrom apps.song.models import Songdef excel_ktvsong(request):
  """
導(dǎo)出excel表格
"""
  _id = request.GET.get('id', 0)
  list_obj = Song.objects.filter(is_delete__exact=False)
 # django orm    if list_obj:
 # 創(chuàng)建工作薄
    ws = Workbook(encoding='utf-8')
    w = ws.add_sheet(u"歌曲列表")
    w.write(0, 0, u"歌曲名稱(chēng)")
    w.write(0, 1, u"歌手")
    # 寫(xiě)入數(shù)據(jù)
    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("無(wú)數(shù)據(jù)")

以上這篇python實(shí)現(xiàn)數(shù)據(jù)導(dǎo)出到excel的示例--普通格式就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持武林站長(zhǎng)站。

發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 永靖县| 宿松县| 安西县| 绥棱县| 嫩江县| 栾城县| 阳信县| 九龙城区| 修水县| 南宫市| 星子县| 芦山县| 微博| 上虞市| 东阳市| 榆树市| 广南县| 巨鹿县| 扎赉特旗| 邳州市| 乌鲁木齐县| 分宜县| 万宁市| 常山县| 同仁县| 东港市| 犍为县| 太仓市| 建昌县| 临朐县| 沛县| 彭州市| 绥棱县| 泸州市| 顺平县| 莆田市| 乐陵市| 始兴县| 邵阳市| 福清市| 龙泉市|