程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长

+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

2023-06(2)

python导出excel

发布于2019-08-07 11:56     阅读(545)     评论(0)     点赞(4)     收藏(1)


python代码


# -*- coding: utf-8 -*-
import StringIO
import xlsxwriter
from django.http import HttpResponse


def export_summary(request):
    key = ['q','w','e']
    value = {'q':'qwe','w':'asd','e':'zxc'}
    get_file_name = 'test'
    return make_excel(key,value,get_file_name)


def make_excel(key,value,get_file_name):
    sio = StringIO.StringIO()
    workbook = xlsxwriter.Workbook(sio)
    worksheet = workbook.add_worksheet()
    # 单元格样式
    header_format= workbook.add_format({
        'text_wrap': True,
        'valign': 'vcenter',
        'align': 'center',
        'border': 1,
        'font_size': 10
    })
    col_len = len(key)
    row_len = len(value)+1
    for c in range(col_len):

        for r in range(row_len):
            if r == 0:
                worksheet.write(r,c,key[c],header_format)
            else:
                worksheet.write(r-1,c,value[key[c]],header_format)
    workbook.close()
    sio.seek(0)
    response = HttpResponse(sio.getvalue(), content_type='APPLICATION/OCTET-STREAM')
    file_name = 'attachment; filename=%s.xlsx' % (get_file_name)
    response['Content-Disposition'] = file_name
    return response
  • 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
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41

js代码(angular)

$scope.export = function () {
        var url = 'export_summary?' + 'report_id=' + $scope.report_id;
        window.open(url)
    }
  • 1
  • 2
  • 3
  • 4
详细导出excel样式参考以下网站

xlsxwriter



所属网站分类: 技术文章 > 博客

作者:前方一片漆黑

链接:https://www.pythonheidong.com/blog/article/10530/13a81c03d1bb72c71953/

来源:python黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

4 0
收藏该文
已收藏

评论内容:(最多支持255个字符)