python - excl converts the data into a table, json and other documents (dajngo - Open the Web page automatically downloads)

Benpian only about how to use.

Specific tablib more detailed usage can refer to blog:  https://blog.csdn.net/liangyuannao/article/details/41476277

 

# Tablib modules have to say is really so.

One. Run the script file into the local

Import be taglib 


headers = ( ' product name ' , ' number ' , ' monovalent ' , ' total ' ) 
Data = [ 
    [ ' Orange ' , ' . 1 ' , ' . 5 ' , ' . 5 ' ], 
    [ ' Apple ' , ' 2 ' , ' 6 ' , '12']
]
data = tablib.Dataset(*data, headers=headers, title="数据")
myfile = open('mydata.xlsx', 'wb')
myfile.write(data.xlsx)
myfile.close()

 

two. After opening the automatic download page.

DEF download_file (Request, * args, ** kwargs): 
    headers = ( ' product name ' , ' number ' , ' monovalent ' , ' total ' ) 
    Data = [ 
        [ ' Orange ' , ' . 1 ' , ' . 5 ' , ' . 5 ' ], 
        [ ' Apple ' , ' 2 ' , '6', ' 12 is ' ] 
    ] 
    Data = tablib.Dataset (* Data, headers = headers, title = " data " ) 

    Response = the HttpResponse (data.xlsx, the content_type = ' file application / vnd.ms-Excel ' ) 
    Response [ ' Content- Disposition ' ] = ' Attachment; filename = fruit sales statistics .xlsx ' 
    return Response

 

Guess you like

Origin www.cnblogs.com/chaoqi/p/11704997.html