Skip to content

Instantly share code, notes, and snippets.

@Apkawa
Created November 7, 2011 10:38
Show Gist options
  • Save Apkawa/1344633 to your computer and use it in GitHub Desktop.
Save Apkawa/1344633 to your computer and use it in GitHub Desktop.
Xls utils
def make_response_for_xls(xls_content, xls_filename):
'''Make xls response by xls_content as binary sttring and xls_filename
from django.http import HttpResponse
response = HttpResponse(xls_content, mimetype="application/ms-excel")
response["Content-Disposition"] = u"attachment; filename=\"{0}\"".format(xls_filename)
return response
# -*- coding: utf-8 -*-
'''
Wrapper on xlswrt like csv.writer
'''
import xlwt
class XLSWriter(object):
_xls = None
_sheet = None
_fileobj = None
_current_row = 0
def __init__(self, fileobj, auto_flush=True):
self._xls = xlwt.Workbook(encoding="utf-8")
self._sheet = self._xls.add_sheet("sheet")
self._fileobj = fileobj
self._auto_flush = auto_flush
def writerow(self, row):
'''
'''
for column, cell in enumerate(row):
self._sheet.write(self._current_row, column, cell)
self._current_row += 1
if self._auto_flush:
self.flush()
def flush(self):
self._fileobj.seek(0)
self._xls.save(self._fileobj)
def writerows(self, rows):
'''
'''
for row in rows:
self.writerow(row)
writer = XLSWriter
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment