django 导出execl代码
首页 > python > Django   作者:littleQ  2017年7月17日 10:26 星期一  热度:1519°  字号:   评论:0 条
时间:2017-7-17 10:26   热度:1519°  评论:0 条 
import xlwt
from io import BytesIO

def fw_template_import():
    response = HttpResponse(content_type='application/vnd.ms-excel')
    mid_name = 'F基础环境防火墙策略'
    response['Content-Disposition'] = 'attachment;filename={0}.xls'.format(mid_name)
    wb = xlwt.Workbook(encoding='utf-8')
    sheet_prd = wb.add_sheet('生产环境')
    sheet_stg = wb.add_sheet('测试环境')
    sheet_dr = wb.add_sheet('灾备环境')

    fw_prd_list = FWTemplate.objects.filter(fw_env='prd')
    fw_stg_list = FWTemplate.objects.filter(fw_env='stg')
    fw_dr_list = FWTemplate.objects.filter(fw_env='dr')
    fw_mod_list = FWTemplate.objects.values("fw_sys_mod", "fw_env").annotate(num=Count("fw_sys_mod"), id=Min("id"))

    style_heading = xlwt.easyxf("""
                        font:
                            name Arial,
                            colour_index white,
                            bold on,
                            height 0xA0;
                        align:
                            wrap off,
                            vert center,
                            horiz center;
                        pattern:
                            pattern solid,
                            fore-colour 0x19;
                        borders:
                            left THIN,
                            right THIN,
                            top THIN,
                            bottom THIN;
                        """
                                )
    style_body = xlwt.easyxf("""
                        font:
                            name Arial,
                            bold off,
                            height 0XA0;
                        align:
                            wrap on,
                            vert center,
                            horiz left;
                        borders:
                            left THIN,
                            right THIN,
                            top THIN,
                            bottom THIN;
                        """
                             )
    style_green = xlwt.easyxf(" pattern: pattern solid,fore-colour 0x11;")
    style_red = xlwt.easyxf(" pattern: pattern solid,fore-colour 0x0A;")
    fmts = [
        'M/D/YY',
        'D-MMM-YY',
        'D-MMM',
        'MMM-YY',
        'h:mm AM/PM',
        'h:mm:ss AM/PM',
        'h:mm',
        'h:mm:ss',
        'M/D/YY h:mm',
        'mm:ss',
        '[h]:mm:ss',
        'mm:ss.0',
    ]
    style_body.num_format_str = fmts[0]

    # 1st line
    row0 = [u'子系统', u'INVOKE-LE', u'SERVER-IP', u'INVOKED-SERVICE', u'SERVER-IP', u'端口信息', u'描述', u'备注']
    for i in range(0, len(row0)):
        sheet_prd.write(0, i, row0[i], style_heading)
        sheet_stg.write(0, i, row0[i], style_heading)
        sheet_dr.write(0, i, row0[i], style_heading)
    # sheet_prd.write(0, 0, '子系统', style_heading)
    # sheet_prd.write(0, 1, 'INVOKE-LE', style_heading)
    # sheet_prd.write(0, 2, 'SERVER-IP', style_heading)
    # sheet_prd.write(0, 3, 'INVOKED-SERVICE', style_heading)
    # sheet_prd.write(0, 4, 'SERVER-IP', style_heading)
    # sheet_prd.write(0, 5, '端口信息', style_heading)
    # sheet_prd.write(0, 6, '描述', style_heading)
    # sheet_prd.write(0, 7, '备注', style_heading)

    # write to prd
    row = 1
    for content in fw_prd_list:
        for fw_mod in fw_mod_list:
            if fw_mod['id'] == content.id:
                sheet_prd.write_merge(1, row + fw_mod['num'] - 1, 0, 0, content.fw_sys_mod.sys_mod_enname, style_body)

        sheet_prd.write(row, 1, content.invoke_le, style_body)
        if ServiceInstance.objects.filter(service_env='prd', service_name=content.invoke_le):
            invoke_ip = ServiceInstance.objects.filter(service_env='prd',
                                                       service_name=content.invoke_le).first().service_ip
        else:
            invoke_ip = ''
        sheet_prd.write(row, 2, invoke_ip, style_body)
        sheet_prd.write(row, 3, content.invoked_service, style_body)
        if ServiceInstance.objects.filter(service_env='prd',
                                          service_name=content.invoked_service):
            invoked_ip = ServiceInstance.objects.filter(service_env='prd',
                                                        service_name=content.invoked_service).first().service_ip
            invoked_port = ServiceInstance.objects.filter(service_env='prd',
                                                          service_name=content.invoked_service).first().service_port
        else:
            invoked_ip = ''
            invoked_port = ''
        sheet_prd.write(row, 4, invoked_ip, style_body)
        sheet_prd.write(row, 5, invoked_port, style_body)
        sheet_prd.write(row, 6, content.fw_des, style_body)
        sheet_prd.write(row, 7, content.fw_opt, style_body)

        # 第一行加宽
        sheet_prd.col(0).width = 100 * 50
        sheet_prd.col(1).width = 200 * 50
        sheet_prd.col(2).width = 100 * 50
        sheet_prd.col(3).width = 200 * 50
        sheet_prd.col(4).width = 100 * 50
        sheet_prd.col(5).width = 100 * 50
        sheet_prd.col(6).width = 100 * 50
        sheet_prd.col(7).width = 100 * 50

        row += 1
    # write to stg
    row = 1
    for content in fw_stg_list:
        for fw_mod in fw_mod_list:
            if fw_mod['id'] == content.id:
                sheet_stg.write_merge(1, row + fw_mod['num'] - 1, 0, 0, content.fw_sys_mod.sys_mod_enname, style_body)
        sheet_stg.write(row, 1, content.invoke_le, style_body)
        if ServiceInstance.objects.filter(service_env='prd', service_name=content.invoke_le):
            invoke_ip = ServiceInstance.objects.filter(service_env='prd',
                                                       service_name=content.invoke_le).first().service_ip
        else:
            invoke_ip = ''
        sheet_stg.write(row, 2, invoke_ip, style_body)
        sheet_stg.write(row, 3, content.invoked_service, style_body)
        if ServiceInstance.objects.filter(service_env='prd',
                                          service_name=content.invoked_service):
            invoked_ip = ServiceInstance.objects.filter(service_env='prd',
                                                        service_name=content.invoked_service).first().service_ip
            invoked_port = ServiceInstance.objects.filter(service_env='prd',
                                                          service_name=content.invoked_service).first().service_port
        else:
            invoked_ip = ''
            invoked_port = ''
        sheet_stg.write(row, 4, invoked_ip, style_body)
        sheet_stg.write(row, 5, invoked_port, style_body)
        sheet_stg.write(row, 6, content.fw_des, style_body)
        sheet_stg.write(row, 7, content.fw_opt, style_body)

        # 第一行加宽
        sheet_stg.col(0).width = 100 * 50
        sheet_stg.col(1).width = 200 * 50
        sheet_stg.col(2).width = 100 * 50
        sheet_stg.col(3).width = 200 * 50
        sheet_stg.col(4).width = 100 * 50
        sheet_stg.col(5).width = 100 * 50
        sheet_stg.col(6).width = 100 * 50
        sheet_stg.col(7).width = 100 * 50

        row += 1
    # write to dr
    row = 1
    for content in fw_stg_list:
        for fw_mod in fw_mod_list:
            if fw_mod['id'] == content.id:
                sheet_dr.write_merge(1, row + fw_mod['num'] - 1, 0, 0, content.fw_sys_mod.sys_mod_enname,
                                      style_body)
        sheet_dr.write(row, 1, content.invoke_le, style_body)
        if ServiceInstance.objects.filter(service_env='prd', service_name=content.invoke_le):
            invoke_ip = ServiceInstance.objects.filter(service_env='prd',
                                                       service_name=content.invoke_le).first().service_ip
        else:
            invoke_ip = ''
        sheet_dr.write(row, 2, invoke_ip, style_body)
        sheet_dr.write(row, 3, content.invoked_service, style_body)
        if ServiceInstance.objects.filter(service_env='prd',
                                          service_name=content.invoked_service):
            invoked_ip = ServiceInstance.objects.filter(service_env='prd',
                                                        service_name=content.invoked_service).first().service_ip
            invoked_port = ServiceInstance.objects.filter(service_env='prd',
                                                          service_name=content.invoked_service).first().service_port
        else:
            invoked_ip = ''
            invoked_port = ''
        sheet_dr.write(row, 4, invoked_ip, style_body)
        sheet_dr.write(row, 5, invoked_port, style_body)
        sheet_dr.write(row, 6, content.fw_des, style_body)
        sheet_dr.write(row, 7, content.fw_opt, style_body)

        # 第一行加宽
        sheet_dr.col(0).width = 100 * 50
        sheet_dr.col(1).width = 200 * 50
        sheet_dr.col(2).width = 100 * 50
        sheet_dr.col(3).width = 200 * 50
        sheet_dr.col(4).width = 100 * 50
        sheet_dr.col(5).width = 100 * 50
        sheet_dr.col(6).width = 100 * 50
        sheet_dr.col(7).width = 100 * 50

        row += 1

    output = BytesIO()
    wb.save(output)
    output.seek(0)
    response.write(output.getvalue())
    return response
 您阅读这篇文章共花了: 
捐赠支持:如果觉得这篇文章对您有帮助,请“扫一扫”鼓励作者!
二维码加载中...
本文作者:littleQ      文章标题: django 导出execl代码
本文地址:http://www.hylinux.cn/python_django/85.html
版权声明:若无注明,本文皆为“浮华”原创,转载请保留文章出处。

返回顶部    首页    手气不错    手机版本    About Me    私人小窝   
版权所有:浮华    站长: littleQ   粤ICP备15005816