Created
November 7, 2011 10:38
-
-
Save Apkawa/1344633 to your computer and use it in GitHub Desktop.
Xls utils
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- 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