[Django] data export excel enhanced version (very powerful !), Django data export
Not to mention, the principle is to use xlwt to export excel files. The so-called forced version refers to the implementation of selecting certain conditions on the webpage to export the corresponding data.
I have published such articles in my blog post before, but I only want to export data. This time, I thought about it and added the online search to finally find out how to implement the conditional export function.
First:
Note: The core is to use the naming group in the django view, for example, configuring url (R' ^ perm = (? P <a> \ w +) $ ', 'keywork. views. work)
Then, I can access the URL/perm = parameter value. This type of parameter value is our condition. Finally, compile a click function in javascript to pop up the corresponding link.
Post Code:
HTML code in the template:
<Div class = "tab-pane" id = "data_export"> <! -- Tab -- 4 -- data export -->
JQUERY code in the template:
$ ('# Sclj '). click (function () {// data export link tj1 =1 ("# ny "). val (); tj2 = $ ("# zj "). val (); tj3 = $ ("# cpmc "). val (); tj4 = $ ("# yhlx "). val (); $ ('# work_backup '). attr ('href ', 'backup-' + tj1 + '-' + tj2 + '-' + tj3 + '-' + tj4); $ ('# work_backup '). text ('backup-'+ tj1 +'-'+ tj2 +'-'+ tj3 +'-'+ tj4 );});
View code:
Def work_backup (request, a, B, c, d): response = HttpResponse (content_type = 'application/vnd. ms-excel ') response ['content-disposition'] = 'attachment; done' workbook = xlwt. workbook (encoding = 'utf-8') # create a workbook sheet = Workbook. add_sheet ("sheet1") # create a work page row0 = [u'user id', u'device status', u'user number', u'user name ', u'account id', u'product id', u'bureau name', u'bureau id', u'region name', u'region id ', u'grid name', u'salesperson name', u'sales point name', u'number completion time', u'number disassemble time', u'user type ', u'other product identifiers ', u'service provider id', u'service provider name', u'crm completion time', u'crm accepted employees ', u'crm acceptance employee No. ', u'acceptance point', u'poor sales name', u'statistical time'] for I in range (0, len (row0 )): sheet. write (0, I, row0 [I]) if a = "all": a = "" if B = "all ": B = "" if c = "all": c = "" if d = "all": d = "" data = DevData. objects. filter (Q (day_id _ contains = a) & Q (mkt_chnl_id _ contains = B) & Q (product_id _ contains = c) & Q (user_flag _ contains = d )). values () num = 1 for d in data: sheet. write (num, 0, d ['serv _ id']) sheet. write (num, 1, d ['serv _ state_name ']) sheet. write (num, 2, d ['Acc _ nbr ']) sheet. write (num, 3, d ['user _ name']) sheet. write (num, 4, d ['acct _ Code']) sheet. write (num, 5, d ['product _ id']) sheet. write (num, 6, d ['mkt _ chnl_name ']) sheet. write (num, 7, d ['mkt _ chnl_id ']) sheet. write (num, 8, d ['mkt _ region_name ']) sheet. write (num, 9, d ['mkt _ region_id ']) sheet. write (num, 10, d ['mkt _ grid_name ']) sheet. write (num, 11, d ['sale _ man ']) sheet. write (num, 12, d ['sale _ outlets_cd1_name ']) sheet. write (num, 13, d ['completed _ time']) sheet. write (num, 14, d ['remove _ data']) sheet. write (num, 15, d ['user _ flag']) sheet. write (num, 16, d ['Pro _ flag']) sheet. write (num, 17, d ['service _ offer_id ']) sheet. write (num, 18, d ['service _ offer_name ']) sheet. write (num, 19, d ['finish _ time']) sheet. write (num, 20, d ['staff _ name']) sheet. write (num, 21, d ['staff _ Code']) sheet. write (num, 22, d ['org _ name']) sheet. write (num, 23, d ['prod _ offer_name ']) sheet. write (num, 24, d ['day _ id']) num = num + 1 workbook. save (response) return response
Code in url:
url(r'^backup-(?P<a>\w+)-(?P<b>\w+)-(?P<c>\w+)-(?P<d>\w+)$', 'keywork.views.work_backup'),