django xlwt实现资产导出功能

做个记录 views

import xlwt
class ExAssetView(LoginRequiredMixin,View):
    def get(self,request):
        row = 1
        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 ocean_blue;
                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;
                """)
        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_green = xlwt.easyxf(" pattern: pattern solid,fore-colour 0x11;")
        style_red = xlwt.easyxf(" pattern: pattern solid,fore-colour 0x0A;")
        style_body.num_format_str = fmts[0]
        ass_all = Asset.objects.all()
        response = HttpResponse(content_type='application/vnd.ms-excel')#这里响应对象获得了一个特殊的mime类型,告诉浏览器这是个exell文件不是html
        response['Content-Disposition'] = 'attachment; filename=asset'+time.strftime('%Y%m%d',time.localtime(time.time()))+'.xls'#这里响应对象获得了附加的Content-Disposition协议头,它含有excel文件的名称,文件名随意,当浏览器访问它时,会以"另存为"对话框中使用它
        f = xlwt.Workbook()  # 创建工作簿
        sheet1 = f.add_sheet(u'sheet1', cell_overwrite_ok=True)  # 创建sheet
        # sheet1.write(0, 1, label='内网ip', style_heading)
        sheet1.write(0, 0, '主机名', style_heading)
        sheet1.write(0, 1, '内网ip', style_heading)
        sheet1.write(0, 2, '外网ip', style_heading)
        sheet1.write(0, 3, '端口', style_heading)
        sheet1.write(0, 4, '总内存', style_heading)
        sheet1.write(0, 5, '总磁盘', style_heading)
        sheet1.write(0, 6, 'CPU型号', style_heading)
        sheet1.write(0, 7, 'CPU核数', style_heading)
        sheet1.write(0, 8, '系统版本', style_heading)
        sheet1.write(0, 9, '系统发行版本', style_heading)
        sheet1.write(0, 10, 'DNS', style_heading)
        sheet1.write(0, 11, 'MAC地址', style_heading)
        sheet1.write(0, 12, '内核版本', style_heading)
        sheet1.write(0, 13, '序列号', style_heading)
        sheet1.write(0, 14, '虚拟化', style_heading)
        sheet1.write(0, 15, '状态', style_heading)
        sheet1.write(0, 16, '系统用户', style_heading)
        sheet1.write(0, 17, '产品线', style_heading)
        sheet1.write(0, 18, '标签', style_heading)
        sheet1.write(0, 19, '云平台', style_heading)
        sheet1.write(0, 20, '创建用户', style_heading)
        sheet1.write(0, 21, '备注', style_heading)
        sheet1.write(0, 22, '创建时间', style_heading)
        sheet1.write(0, 23, '更新时间', style_heading)
        for ass in ass_all:
            sheet1.write(row, 0, ass.hostname)
            sheet1.write(row, 1, ass.inner_ip)
            sheet1.write(row, 2, ass.pub_ip)
            sheet1.write(row, 3, ass.port)
            sheet1.write(row, 4, ass.mem_total)
            sheet1.write(row, 5, ass.disk_total)
            sheet1.write(row, 6, ass.cpu_model)
            sheet1.write(row, 7, ass.num_cpus)
            sheet1.write(row, 8, ass.osfinger)
            sheet1.write(row, 9, ass.osrelease)
            sheet1.write(row, 10, ass.dns)
            sheet1.write(row, 11, ass.mac_addr)
            sheet1.write(row, 12, ass.kernelrelease)
            sheet1.write(row, 13, ass.serialnumber)
            sheet1.write(row, 14, ass.virtual)
            if ass.status == '正常':

                sheet1.write(row, 15, ass.status,style_green)
            else:
                sheet1.write(row, 15, ass.status, style_red)

            sheet1.write(row, 16, ass.system_user.name + '--' + ass.system_user.username)
            sheet1.write(row, 17, ass.product.name)
            for tags in ass.tag.all():
                sheet1.write(row, 18, tags.name)
            sheet1.write(row, 19, ass.cloud_platform.cloud)
            sheet1.write(row, 20, ass.create_user)
            sheet1.write(row, 21, ass.detail)
            sheet1.write(row, 22, str(ass.create_time), style_body)
            sheet1.write(row, 23, str(ass.update_time), style_body)

            row += 1
        f.save(response)#写入表格
        return response

urls

from  .views import ExAssetView
 # 资产导出
app_name = 'asset'
urlpatterns = [
url(r'ex_asset/$', ExAssetView.as_view(), name='ex_asset'),
]

前端模板

  <a class="btn btn-default btn_import" data-toggle="modal" data-target="#user_import_modal"
                       tabindex="0">
                        <span><a href="{% url 'asset:ex_asset' %}" class="btn btn-primary btn-sm" type="button">导出</a></span>
                    </a>

导出表格样式

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏闻道于事

js登录滑动验证,不滑动无法登陆

js的判断这里是根据滑块的位置进行判断,应该是用一个flag判断 <%@ page language="java" contentType="text/html...

7278
来自专栏Golang语言社区

【Golang语言社区】GO1.9 map并发安全测试

var m sync.Map //全局 func maintest() { // 第一个 YongHuomap := make(map[st...

4908
来自专栏张善友的专栏

LINQ via C# 系列文章

LINQ via C# Recently I am giving a series of talk on LINQ. the name “LINQ via C...

2675
来自专栏pangguoming

Spring Boot集成JasperReports生成PDF文档

由于工作需要,要实现后端根据模板动态填充数据生成PDF文档,通过技术选型,使用Ireport5.6来设计模板,结合JasperReports5.6工具库来调用渲...

1.3K7
来自专栏Ceph对象存储方案

Luminous版本PG 分布调优

Luminous版本开始新增的balancer模块在PG分布优化方面效果非常明显,操作也非常简便,强烈推荐各位在集群上线之前进行这一操作,能够极大的提升整个集群...

3265
来自专栏杨龙飞前端

scrollto 到指定位置

2564
来自专栏我和未来有约会

Kit 3D 更新

Kit3D is a 3D graphics engine written for Microsoft Silverlight. Kit3D was inita...

2636
来自专栏转载gongluck的CSDN博客

cocos2dx 打灰机

#include "GamePlane.h" #include "PlaneSprite.h" #include "BulletNode.h" #include...

5736
来自专栏跟着阿笨一起玩NET

c#实现打印功能

2952
来自专栏一个会写诗的程序员的博客

Spring Reactor 项目核心库Reactor Core

Non-Blocking Reactive Streams Foundation for the JVM both implementing a Reactiv...

2242

扫码关注云+社区