Skip to content

Commit

Permalink
增加Excel xlsx格式导出数据,可以支持大数据量导出
Browse files Browse the repository at this point in the history
  • Loading branch information
marshalys committed Feb 27, 2014
1 parent d51e067 commit 8956303
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 7 deletions.
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@
'django-crispy-forms>=1.4.0',
],
extras_require={
'Excel': ['xlwt'],
'Excel': ['xlwt', 'xlsxwriter'],
'Reversion': ['django-reversion'],
},
zip_safe=True,
zip_safe=False,
keywords=['admin', 'django', 'xadmin', 'bootstrap'],
classifiers=[
'Development Status :: 4 - Beta',
Expand Down
55 changes: 50 additions & 5 deletions xadmin/plugins/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,23 @@
except:
has_xlwt = False

try:
import xlsxwriter
has_xlsxwriter = True
except:
has_xlsxwriter = False


class ExportMenuPlugin(BaseAdminPlugin):

list_export = ('xls', 'csv', 'xml', 'json')
export_names = {'xls': 'Excel', 'csv': 'CSV', 'xml': 'XML', 'json': 'JSON'}
list_export = ('xlsx', 'xls', 'csv', 'xml', 'json')
export_names = {'xlsx': 'Excel 2007', 'xls': 'Excel', 'csv': 'CSV',
'xml': 'XML', 'json': 'JSON'}

def init_request(self, *args, **kwargs):
self.list_export = [
f for f in self.list_export if f != 'xls' or has_xlwt]
f for f in self.list_export
if (f != 'xlsx' or has_xlsxwriter) and (f != 'xls' or has_xlwt)]

def block_top_toolbar(self, context, nodes):
if self.list_export:
Expand All @@ -42,14 +50,14 @@ def block_top_toolbar(self, context, nodes):

class ExportPlugin(BaseAdminPlugin):

export_mimes = {'xls': 'application/vnd.ms-excel', 'csv': 'text/csv',
export_mimes = {'xlsx': 'application/vnd.ms-excel',
'xls': 'application/vnd.ms-excel', 'csv': 'text/csv',
'xml': 'application/xhtml+xml', 'json': 'application/json'}

def init_request(self, *args, **kwargs):
return self.request.GET.get('_do_') == 'export'

def _format_value(self, o):
value = None
if (o.field is None and getattr(o.attr, 'boolean', False)) or \
(o.field and isinstance(o.field, (BooleanField, NullBooleanField))):
value = o.value
Expand All @@ -75,6 +83,43 @@ def _get_datas(self, context):
new_rows.insert(0, [force_unicode(c.text) for c in context['result_headers'].cells if c.export])
return new_rows

def get_xlsx_export(self, context):
datas = self._get_datas(context)
output = StringIO.StringIO()
export_header = (
self.request.GET.get('export_xlsx_header', 'off') == 'on')

model_name = self.opts.verbose_name
book = xlsxwriter.Workbook(output)
sheet = book.add_worksheet(
u"%s %s" % (_(u'Sheet'), force_unicode(model_name)))
styles = {'datetime': book.add_format({'num_format': 'yyyy-mm-dd hh:mm:ss'}),
'date': book.add_format({'num_format': 'yyyy-mm-dd'}),
'time': book.add_format({'num_format': 'hh:mm:ss'}),
'header': book.add_format({'font': 'name Times New Roman', 'color': 'red', 'bold': 'on', 'num_format': '#,##0.00'}),
'default': book.add_format()}

if not export_header:
datas = datas[1:]
for rowx, row in enumerate(datas):
for colx, value in enumerate(row):
if export_header and rowx == 0:
cell_style = styles['header']
else:
if isinstance(value, datetime.datetime):
cell_style = styles['datetime']
elif isinstance(value, datetime.date):
cell_style = styles['date']
elif isinstance(value, datetime.time):
cell_style = styles['time']
else:
cell_style = styles['default']
sheet.write(rowx, colx, value, cell_style)
book.close()

output.seek(0)
return output.getvalue()

def get_xls_export(self, context):
datas = self._get_datas(context)
output = StringIO.StringIO()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ <h4 class="modal-title">{% trans "Export" %} {{et.name}}</h4>
{{ form_params|safe }}
<input type="hidden" name="export_type" value="{{et.type}}">
<label class="checkbox">
{% if et.type == "xlsx" %}
<input type="checkbox" name="export_xlsx_header" checked="checked" value="on"> {% trans "Export with table header." %}
{% endif %}
{% if et.type == "xls" %}
<input type="checkbox" name="export_xls_header" checked="checked" value="on"> {% trans "Export with table header." %}
{% endif %}
Expand Down

0 comments on commit 8956303

Please sign in to comment.