# coding:utf-8
from django.http import HttpResponse
from xlwt import *
import StringIO, os
from test_celery.models import PhoneNumber

def excel_out(request):
    number_list = PhoneNumber.objects.all().order_by('time')
    if number_list:
        ws = Workbook(encoding='utf-8')
        w = ws.add_sheet(u'电话本', cell_overwrite_ok=True)
        w.write(0, 0, u'编号')
        w.write(0, 1, u'姓名')
        w.write(0, 2, u'性别')
        w.write(0, 3, u'时间')
        excel_row = 1
        for number in number_list:
            id = number.id
            name = number.name
            sex = number.sex
            time = str(number.time)
            w.write(excel_row, 0, id)
            w.write(excel_row, 1, name)
            w.write(excel_row, 2, sex)
            w.write(excel_row, 3, time)
            excel_row += 1

        # 保存于本地
        # exist_file = os.path.exists('phone.xls')
        # if exist_file:
        #     os.remove(r'phone.xls')
        # ws.save('phone.xls')

        # 返回文件给用户,用户操作浏览器对话框保存文件
        sio = StringIO.StringIO()
        ws.save(sio)
        sio.seek(0)
        # http响应头告知浏览器,返回excel
        response = HttpResponse(sio.getvalue(), content_type='application/vnd.ms-excel')
        # 浏览器打开/保存的对话框
        response['Content-Disposition'] = 'attachment;filename=phone.xls'
        # 响应体
        response.write(sio.getvalue())
        return response

django导出excel的更多相关文章

  1. django 导出excel react下载 --- 导出并下载

    Dajngo查询数据,查询出来之后生成Excel保存本地 class ExportExcel(APIView): def post(self, request, *args, **kwargs): e ...

  2. Django导出excel中文乱码解决方案

    Django官方文档有关于怎么生成csv文件的方法 import csv from django.http import HttpResponse def some_view(request): # ...

  3. [django]数据导出excel升级强化版(很强大!)

    不多说了,原理采用xlwt导出excel文件,所谓的强化版指的是实现在网页上选择一定条件导出对应的数据 之前我的博文出过这类文章,但只是实现导出数据,这次左思右想,再加上网上的搜索,终于找出方法实现条 ...

  4. django页面导出excel

    from django.http import HttpResponse from xlwt import * from io import BytesIO def excel_export(requ ...

  5. django导入导出excel实践

    一.xlrd和xlwt模块介绍 xlrd模块提供在任何平台上从excel电子表格(.xls和.xlsx)中提取数据的功能,xlwt模块提供生成与Microsoft Excel 95 到2003版本兼容 ...

  6. django+uwsgi+nginx 导出excel超时问题

    一.问题现象和日志报错 之前在项目实现了excel导入导出:django导入导出excel实践,之前一直稳定运行,突然得知导出用户信息时出现nginx错误报告: 查看nginx日志,报错信息如下所示: ...

  7. Django Admin中增加导出Excel功能

    参考: https://www.cnblogs.com/yoyo008/p/9232805.html 在使用Django Admin时, 对于列表我们有时需要提供数据导出功能, 如下图: 在Djang ...

  8. 【django】 django后台管理 导出excel表

    from django.contrib import admin # Register your models here. import xlwt from django.http import Ht ...

  9. C#使用Aspose.Cells导出Excel简单实现

    首先,需要添加引用Aspose.Cells.dll,官网下载地址:http://downloads.aspose.com/cells/net 将DataTable导出Xlsx格式的文件下载(网页输出) ...

随机推荐

  1. TIP协议

    1. TIP是什么? CISCO给TIP的定义如下: The TIP protocol specifications describe how to multiplex multiple screen ...

  2. [转]CMake快速入门教程:实战

    转自http://blog.csdn.net/ljt20061908/article/details/11736713 0. 前言    一个多月前,由于工程项目的需要,匆匆的学习了一下cmake的使 ...

  3. java连接redis无法连接,报异常RedisConnectionException

    不管是spring还是原生jedis连接redis,如果连不上多半是linux服务器的问题: 1 首先确保redis端口开放: 把6379或者redis的端口开放即可 2 redis.conf配置注释 ...

  4. 【洛谷】【洛谷月赛】4月月赛Round 1/2

    洛谷月赛“月”来“月”丧了,一月更比一月丧,做得我十分不“月”…… 4月的两轮月赛,都只会T1,就写一下吧,等待后续更新…… 先看看Round1的T1: [R1T1] 网址:点我 [题意简述] 给定一 ...

  5. [how to]HBase Snapshots原理与使用

    1.简介 Snapshots即快照的意思,作用于表上.在对于表做快照的时候不会造成文件的拷贝,如不会对HFile文件进行拷贝而是以链接的方式链接到元表的HFile上.可以说它是一种元数据的集合,可以快 ...

  6. 20 Organizing Go code 组织go代码

    Organizing Go code 16 August 2012 Introduction Go code is organized differently to that of other lan ...

  7. Windows 8 应用程序前后台切换事件监听

    在一些情况下,我们需要监听应用程序切换到后台或者从后台切换至前台的事件,从而进行相关处理操作.支付宝应用锁屏(IOS,Android平台)的处理中就需要监听此事件,在用户将应用切换至后台一段时间后再切 ...

  8. XShell安装

    Xshell就是一个远程控制Centos的软件:(用XShell比较方便,试用的都知道,界面也人性化) 详细介绍请看 百度百科 下面我们来安装下这个工具: 双击exe 点下一步: 选 免费的 然后下一 ...

  9. NopCommerce Plugins 不能智能提示的解决方法(MVC 5 & RAZOR 3.0)

    分享给需要的朋友: http://mhammadchehab.com/wordpress/2013/12/enabling-intellisense-for-razor-in-class-librar ...

  10. ***codeigniter操作xml(Simplexml第三方扩展)

    This Simplexml class provides an alternative implementation of the SimpleXML API that works under PH ...