2020-12-07 07:23:05 +00:00
|
|
|
import pyexcel
|
2023-03-13 10:06:54 +00:00
|
|
|
from django.utils.translation import gettext as _
|
2023-03-13 09:57:50 +00:00
|
|
|
|
2020-12-07 07:23:05 +00:00
|
|
|
from .base import BaseFileParser
|
|
|
|
|
|
|
|
|
|
|
|
class ExcelFileParser(BaseFileParser):
|
|
|
|
media_type = 'text/xlsx'
|
|
|
|
|
|
|
|
def generate_rows(self, stream_data):
|
2023-03-13 09:57:50 +00:00
|
|
|
try:
|
|
|
|
workbook = pyexcel.get_book(file_type='xlsx', file_content=stream_data)
|
|
|
|
except Exception:
|
2023-03-13 10:06:54 +00:00
|
|
|
raise Exception(_('Invalid excel file'))
|
2020-12-07 07:23:05 +00:00
|
|
|
# 默认获取第一个工作表sheet
|
|
|
|
sheet = workbook.sheet_by_index(0)
|
|
|
|
rows = sheet.rows()
|
|
|
|
return rows
|