Browse Source

[IMP] float field as float cell in XLS

pull/2/head
Cristian Salamea 12 years ago
parent
commit
877c7bb61b
  1. 31
      web_export_view/controllers.py

31
web_export_view/controllers.py

@ -18,6 +18,10 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
# #
############################################################################## ##############################################################################
import re
import xlwt
from cStringIO import StringIO
try: try:
import json import json
except ImportError: except ImportError:
@ -31,6 +35,33 @@ from web.controllers.main import ExcelExport
class ExcelExportView(ExcelExport): class ExcelExportView(ExcelExport):
_cp_path = '/web/export/xls_view' _cp_path = '/web/export/xls_view'
def from_data(self, fields, rows):
workbook = xlwt.Workbook()
worksheet = workbook.add_sheet('Sheet 1')
for i, fieldname in enumerate(fields):
worksheet.write(0, i, fieldname)
worksheet.col(i).width = 8000 # around 220 pixels
style = xlwt.easyxf('align: wrap yes')
for row_index, row in enumerate(rows):
for cell_index, cell_value in enumerate(row):
if isinstance(cell_value, basestring):
cell_value = re.sub("\r", " ", cell_value)
if re.match('^[\d,]+(\.\d+)?$', cell_value):
cell_value = float(cell_value.replace(',',''))
style = xlwt.easyxf(num_format_str='#,##0.00')
if cell_value is False: cell_value = None
worksheet.write(row_index + 1, cell_index, cell_value, style)
fp = StringIO()
workbook.save(fp)
fp.seek(0)
data = fp.read()
fp.close()
return data
@openerpweb.httprequest @openerpweb.httprequest
def index(self, req, data, token): def index(self, req, data, token):
data = json.loads(data) data = json.loads(data)

Loading…
Cancel
Save