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>
导出表格样式

django xlwt实现资产导出功能的更多相关文章
- 在django里用xlwt作EXCEL导出功能
参考了以前kevin的代码,搞起来快呀,哈哈哈,,阿里亚多.... 代码的意思比较明白,不注释,几乎自解释... 就是定义EXCEL时,比较硬生生的,一步一步没有快捷的方法. 另外就是取时间出来时,如 ...
- Django Admin中增加导出Excel功能
参考: https://www.cnblogs.com/yoyo008/p/9232805.html 在使用Django Admin时, 对于列表我们有时需要提供数据导出功能, 如下图: 在Djang ...
- Django Admin中增加导出CSV功能
参考: https://books.agiliq.com/projects/django-admin-cookbook/en/latest/export.html 在使用Django Admin时, ...
- 用SpringMvc实现Excel导出功能
以前只知道用poi导出Excel,最近用了SpringMvc的Excel导出功能,结合jxl和poi实现,的确比只用Poi好,两种实现方式如下: 一.结合jxl实现: 1.引入jxl的所需jar包: ...
- 【HOW】如何限制Reporting Services报表导出功能中格式选项
Reporting Services报表导出功能中缺省会提供多种导出格式选项,但很多情况下不需要全部的格式选项,因此需要对这些选项进行限制.下面我们以SQL Server 2008 R2为例来说明对这 ...
- Atitit.excel导出 功能解决方案 php java C#.net版总集合.doc
Atitit.excel导出 功能解决方案 php java C#.net版总集合.docx 1.1. Excel的保存格式office2003 office2007/2010格式1 1.2. 类库选 ...
- 利用Aspose.Cells完成easyUI中DataGrid数据的Excel导出功能
我准备在项目中实现该功能之前,google发现大部分代码都是利用一般处理程序HttpHandler实现的服务器端数据的Excel导出,但是这样存在的问题是ashx读取的数据一般都是数据库中视图的数据, ...
- C#导入、导出功能
//导出功能 protected void btnExport(object sender, EventArgs e) { //用来打开下载窗口 string fileName = "中心联 ...
- [转载]ecshop 实现订单导出功能 指定订单导出 EXCEL 数据文件
当下很多功能都觉得理所当然,但是实际作为2012年停更的ECSHOP来说,很多功能其实都是缺少的,好比今天的要说的功能 订单导出 这个功能对于现在的产品设计来说,应该属于一个比较常规的功能,但是ECS ...
随机推荐
- Appium 学习一:环境搭建问题
1.安装Android-sdk http://tools.android-studio.org/index.php/sdk 问题1:下载 android-sdk_r24.4.1-windows.zip ...
- Apache无法正常启动(配置多个监听端口)
Apache监测多个端口配置: 1.conf->extra->httpd-vhosts.conf 检查配置项是否写错 2.http.conf listen端口是否监听正确 3.环境变量中 ...
- 微信小程序记账本进度六
//app.jsApp({ onLaunch: function () { //调用API从本地缓存中获取数据 var logs = wx.getStorageSync('logs') || [] l ...
- svn 清理失败的解决方法
首先 下载 SQLiteSpy 工具, 解压后如下图所示, 打开 .exe 应用程序,File ,opendatabase 选中wc.db 然后执行 delete from work_qu ...
- Java 深拷贝,浅拷贝
一直听说这两个词,确实不知道代表啥意思?也不知道究竟要用来做什么?什么时候用到他们. 下面是从一篇博文种得到的解释: 浅复制(浅克隆) :被复制对象的所有变量都含有与原来的对象相同的值,而所有的对其他 ...
- maven打包不包含配置文件[z]
如果使用maven-jar-plugin和maven-dependency-plugin打包,排除配置文件的方法: 上面的配置是把resources下的配置文件不打进项目的jar包中,主要使用的是&l ...
- 【Spark2.0源码学习】-7.Driver与DriverRunner
承接上一节内容,Client向Master发起RequestSubmitDriver请求,Master将DriverInfo添加待调度列表中(waitingDrivers),下面针对于Dri ...
- PHPNow升级PHP版本
PHPNow升级PHP版本 phpnow下载地址:http://www.jb51.net/softs/12868.html 1,先把PHP5.3.5下载下来,在官网我是没找到VC6的版本,只能从Goo ...
- 为什么说Redis是单线程的以及Redis为什么这么快!
参考文章:https://blog.csdn.net/xlgen157387/article/details/79470556 redis简介 Redis是一个开源的内存中的数据结构存储系统,它可以用 ...
- Python 多进程编程之 进程间的通信(Queue)
Python 多进程编程之 进程间的通信(Queue) 1,进程间通信Process有时是需要通信的,操作系统提供了很多机制来实现进程之间的通信,而Queue就是其中的一个方法----这是操作系统开辟 ...