Posting mainly for future viewers. Currently, django_import_export
imports only the active/first sheet of a single excel workbook. However, the code is easy enough to modify and alleviate this problem. In forms.py, there is ImportForm
which is the one used while importing from admin. Simply change the import_file
field to something like this:
import_file = forms.FileField(widget=forms.ClearableFileInput(attrs={'multiple':
True}),
label=_('File to import')
)
This form is used in admin.py to process the file data. Change the linked line to something like:
import_files = request.FILES.getlist('import_file')
for import_file in import_files:
...
Now all that's left is to modify the import procedure in base_formats.py for XLS and XLSX formats. The changes will be nearly same for both, I will outline the XLS
one here.
Instead of taking the first sheet, run a for
loop over the sheets and append the data to the dataset.
dataset = tablib.Dataset()
first_sheet = True # If you keep all correct headers only in first sheet
for sheet in xls_book.sheets():
if first_sheet:
dataset.headers = sheet.row_values(0)
first_sheet = False
for i in moves.range(1, sheet.nrows):
dataset.append(sheet.row_values(i))
return dataset
For XLSX, the loop will run on xlsx_book.worksheets
. Rest is similar to xls
.
This will allow you to select multiple excel workbooks and import all the sheets for a workbook. I know the ideal solution would be to import a zip
file to create all the data using a single bulk_create
, but this serves well for now.