【问题标题】:Django Import Export, how to import specific sheet in excel?Django Import Export,如何在excel中导入特定工作表?
【发布时间】:2020-11-18 02:30:06
【问题描述】:

使用 Django-Import-Export Module 导入特定表格的正确方法是什么??

或者如果可能的话。工作簿中的所有工作表一张一张...

我参考了他们的文档,但没有多大帮助

https://django-import-export.readthedocs.io/en/latest/

就像我想从多张工作表中将数据导出到一个工作簿一样...

如何实现??

【问题讨论】:

  • this answer 有帮助吗?
  • 你试过xlrd吗?它是处理 excel 文件的好选择

标签: django django-import-export


【解决方案1】:

这是我问题的完整答案

databook = Databook()    
imported_data= databook.load(file.read( ), format= 'xlsx')
        
for dataset in imported_data.sheets():
    print(dataset.title)  # returns the names of the sheets
    print(dataset)  # returns the data in each sheet

这是您可以将多个数据集导出到一个 Excel 文件中的方法

book = tablib.Databook((data1, data2, data3))
    
with open('students.xls', 'wb') as f:
    f.write(book.export('xls'))

documentation

【讨论】:

    【解决方案2】:

    您也可以尝试在 django 上使用 pyexcel_xls 包,它相当容易使用。

    from pyexcel_xls import get_data as xls_get
    def import_excel(request):
        excel_file = request.FILES['file']
        #uploading the excel file
        if (str(excel_file).split('.')[-1] == "xls"):
            data = xls_get(excel_file, column_limit=15)
        elif (str(excel_file).split('.')[-1] == "xlsx"):
            data = xlsx_get(excel_file, column_limit=15)
    
        if (len(data['sheet1']) > 2): #reading of records begins from second row
            name = data['sheet1']  #excel sheet name you wish to get
            for l in range(2, len(name)): #loop through the number of rows in the sheet
                data = name[l][0] #extract the first column's data
    

    【讨论】:

      猜你喜欢
      • 2020-04-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-05
      • 2015-04-26
      • 1970-01-01
      • 2015-07-17
      • 1970-01-01
      相关资源
      最近更新 更多