這篇文章帶給大家的內容是關於django使用外掛程式下載excel的方法,有一定的參考價值,有需要的朋友可以參考一下,希望對你有幫助。
由於目前的資料多是使用pandas或xlwt函式庫實現的。其實沒有那麼的麻煩,因為django有相對應的插件django-excel。
該插件是依賴pyexcel庫寫的。不過,不用專門安裝pyexcel庫,因為在安裝django-excel時會自動將pyexcel裝上。由於pyexcel是一個大函式庫,使用django-excel時不用考慮pyexcel的使用方法,所以就不再這個介紹太多了。在此,還是要多說一句,pyexcel的功能也是很強大的,可以實現資料的可視化,目前已開發出了與flask、django等web框架的對應插件,例如今天介紹的django-excel。
django-excel是支援上傳和下載excel檔案的插件,並且可以將excel檔案以網頁的形式展現出來,也可以將資料存入資料庫。由於,筆者也是剛接觸該插件,而且開發需求目前僅為下載功能,所以本篇只介紹其下載的功能。 (註:實作下載功能,部署的伺服器不需要安裝office)
1、 安裝
pip install django-excel
##安裝時會自動安裝py-io、pyexcel 、pyexcel-webio2、支援django的版本目前已支援到django2.1.1版本。所以,使用目前django最新版的開發者也不用擔心了,因為它支援。 3、 實作下載該外掛支援的資料格式很多,array(二維陣列)、字典、資料庫的表(單張或多張)、django的ORM查詢結果( query sqt)等等。可以產生的檔案類型:csv、tsv、csvz、tsvz、xls、xlsx、xlsm、ods。套件名稱 |
#支援的檔案格式 |
#依賴 |
#Python#版本 |
pyexcel-io |
#csv, csvz , tsv, tsvz |
2.6, 2.7, 3.3, 3.4, 3.5, 3.6 pypy | |
pyexcel-xls |
#xls, xlsx(唯讀), xlsm(唯讀) |
#xlrd, xlwt |
#同上 |
pyexcel-xlsx |
xlsx | #openpyxl |
|
上表是產生對應檔案格式所需安裝的包。
如果要使用二維陣列來產生excel文件,則需return django_excel.make_response_from_array(二維陣列名, 產生的文件類型, status=200)。二維數組每一行代表Excel中對應的行。
若要使用字典產生excel文件,需要return django_excel.make_response_from_dict(字典名, 文件類型, status=200)。鍵名為列名,鍵值為資料。
如果想要使用資料庫的表格(單張)產生excel文件,則需return django_excel.make_response_from_a_table(表名, 文件類型, status=200)
如果想要使用資料庫的表格(多張)產生excel文件,則需return django_excel.make_response_from_tables(表格名稱清單, 文件類型status=200)
如果想要使用查詢集合產生excel文件,則需return django_excel.make_response_from_query_sets(查詢集合名, 所需列對應欄位(清單類型),檔案類型, status=200)。所需列對應欄位的列表裡的順序為Excel列名的順序,且列表裡的元素必須為model裡的變數名稱。
所有函數的通用參數包含file_name 和sheet_name,分別為檔案名稱和Excel的工作薄名。
注意,不論是資料庫表還是查詢集合,都不能包含外鍵否則會報錯,而且只能透過頁面的跳躍實現下載,不能透過ajax請求,否則不會下載。
eg:
models.py
class django_test_1(models.Model): abc = models.CharField(max_length=20,db_column='测试')
views.py
import django_excel as excel def download_excel(request): data_excel =django_test_1.objects.all() column_names = ["abc"] return excel.make_response_from_query_sets(data_excel,column_names, "xlsx",status = 200 ,sheet_name='测试',file_name='测试文件')
以上是django使用外掛程式下載excel的方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

pythonuseshybridapprace,ComminingCompilationTobyTecoDeAndInterpretation.1)codeiscompiledtoplatform-Indepententbybytecode.2)bytecodeisisterpretedbybythepbybythepythonvirtualmachine,增強效率和通用性。

theKeyDifferencesBetnewpython's“ for”和“ for”和“ loopsare:1)” for“ loopsareIdealForiteringSequenceSquencesSorkNowniterations,而2)”,而“ loopsareBetterforConterContinuingUntilacTientInditionIntionismetismetistismetistwithOutpredefinedInedIterations.un

在Python中,可以通過多種方法連接列表並管理重複元素:1)使用 運算符或extend()方法可以保留所有重複元素;2)轉換為集合再轉回列表可以去除所有重複元素,但會丟失原有順序;3)使用循環或列表推導式結合集合可以去除重複元素並保持原有順序。

fasteStmethodMethodMethodConcatenationInpythondependersonListsize:1)forsmalllists,operatorseffited.2)forlargerlists,list.extend.extend()orlistComprechensionfaster,withextendEffaster,withExtendEffers,withextend()withextend()是extextend()asmoremory-ememory-emmoremory-emmoremory-emmodifyinginglistsin-place-place-place。

toInSerteLementIntoApythonList,useAppend()toaddtotheend,insert()foreSpificPosition,andextend()formultiplelements.1)useappend()foraddingsingleitemstotheend.2)useAddingsingLeitemStotheend.2)useeapecificindex,toadapecificindex,toadaSpecificIndex,toadaSpecificIndex,blyit'ssssssslorist.3 toaddextext.3

pythonlistsareimplementedasdynamicarrays,notlinkedlists.1)他們areStoredIncoNtiguulMemoryBlocks,mayrequireRealLealLocationWhenAppendingItems,EmpactingPerformance.2)LinkesedlistSwoldOfferefeRefeRefeRefeRefficeInsertions/DeletionsButslowerIndexeDexedAccess,Lestpypytypypytypypytypy

pythonoffersFourmainMethodStoreMoveElement Fromalist:1)刪除(值)emovesthefirstoccurrenceofavalue,2)pop(index)emovesanderturnsanelementataSpecifiedIndex,3)delstatementremoveselemsbybybyselementbybyindexorslicebybyindexorslice,and 4)

toresolvea“ dermissionded”錯誤Whenrunningascript,跟隨台詞:1)CheckAndAdjustTheScript'Spermissions ofchmod xmyscript.shtomakeitexecutable.2)nesureThEseRethEserethescriptistriptocriptibationalocatiforecationAdirectorywherewhereyOuhaveWritePerMissionsyOuhaveWritePermissionsyYouHaveWritePermissions,susteSyAsyOURHomeRecretectory。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

WebStorm Mac版
好用的JavaScript開發工具

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

SublimeText3漢化版
中文版,非常好用

SublimeText3 Linux新版
SublimeText3 Linux最新版