博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
django 上传,下载excel
阅读量:7009 次
发布时间:2019-06-28

本文共 2313 字,大约阅读时间需要 7 分钟。

hot3.png

关于这里只把示例部分的url.py view.py html模板部分

url.py

from django.conf.urls import include, urlfrom django.contrib import adminfrom mysites.views import index, upload_file, report_down_loadurlpatterns = [    # Examples:    # url(r'^$', 'untitled1.views.home', name='home'),    # url(r'^blog/', include('blog.urls')),    url(r'^admin/', include(admin.site.urls)),    url(r'^index/$', index),    url(r'^upload_file/$', upload_file),    url(r'^report_down_load/$', report_down_load),]

view.py

from django.http import HttpResponsefrom django.shortcuts import render, render_to_responseimport osimport xlrd# Create your views here.from django.template import RequestContextfrom django.views.decorators.csrf import csrf_exemptdef index(request):    return render_to_response("index.html", '', context_instance=RequestContext(request))@csrf_exemptdef upload_file(request):    if request.method == 'POST':        myFile = request.FILES.get('myfile', None)        if not myFile:            return HttpResponse('no file for upload')        excelFile = open(os.path.join('/Users/eruake/Desktop/code/hkwork/untitled1/', myFile.name), 'wb+')        for chunk in myFile.chunks():            excelFile.write(chunk)            excelFile.close()        #        excel = xlrd.open_workbook('/Users/eruake/Desktop/code/hkwork/untitled1/'+myFile.name)        sheet = excel.sheet_by_index(0)        # sheet = excel.sheet_by_name('mywork')        nrows = sheet.nrows        for i in range(nrows):            # get value you can save to database            v = sheet.cell(0, 0).value        return HttpResponse('upload over!')import xlwtimport django.utils.timezone as timezone@csrf_exemptdef report_down_load(request):    wb = xlwt.Workbook()    sheet = wb.add_sheet('report')    filename = timezone.now().strftime("%Y%m%d %H%M%S") + '.xls'    #     # set data    # and set xls file style 这里要说明一点的时,我们可以设置excel模板的样式,可以通过xlrd先读取,再使用xlutils.copy    # 来实现写,但是由于xlutils是不能读取复制样式,如背景颜色 字体样式等 所以我建议直接使用xlwt来控制样式    response = HttpResponse(content_type='application/vnd.ms-excel')    response['Content-Disposition'] = 'attachment; filename=' + filename    wb.save(response)    return response

template 里的index.html

    
Title

------------------------

 

转载于:https://my.oschina.net/u/198124/blog/824780

你可能感兴趣的文章
安装java及环境配置
查看>>
NFS和SAMBA服务备忘录
查看>>
android 短信发送器
查看>>
Logan:美团点评的开源移动端基础日志库
查看>>
获取Java类中所有Field
查看>>
机器学习需要的数学基础
查看>>
puppet自动化运维之类
查看>>
Python在Linux下的Tab补齐
查看>>
部署搭建 Saltstack
查看>>
多备份亮相安卓开发者大会聚焦全球App数据保护
查看>>
sata盘,SSD盘,fusion-IO卡 对比
查看>>
NFS文件服务器使用简介
查看>>
linux文件名通配
查看>>
Laravel 5.2 教程 - 数据填充
查看>>
C++11之右值引用(二):右值引用与移动语义
查看>>
win2003活动目录与网络系列(3)
查看>>
Android Open Sources
查看>>
[转]开源究竟差哪了
查看>>
The current branch is not configured for pull No value for key branch.master.merge found in config
查看>>
vmware workstation 共享磁盘创建
查看>>