Help

2

How to create Excel report in odoo9 ?

Avatar
Jignesh Mehta

Hello


I have create one wizard and take a field binary file for store file name.

While opening wizard, the file name appears from default_get method.


My Code is shown below:-

class wiz_print_excel(models.TransientModel):

    _name = 'wiz.print.excel'


    @api.model

    def default_get(self, fields):

        rec = super(wiz_print_excel, self).default_get(fields)

        rec['name'] = 'Presentation.xls'

        rec['file'] = 'file'

    return rec


    file = fields.Binary('File')

    name = fields.Char(string='File Name', size=32)


    @api.multi

    def print_excel_report(self):

        workbook = xlwt.Workbook()

        worksheet = workbook.add_sheet('Sheet 1')

        worksheet.write_merge(0, 0, 2, 8, 'Lucent Jewels Inc', GREEN_TABLE_HEADER)

        workbook.save('/Downloads/Presentation.xls')


Click on Print button of wizard, File saved this location. But when i click on download, errors was occurred.

It gives different error front end as well as back end.


Front End Error:-

SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data
http://0.0.0.0:8069/web/static/src/js/framework/ajax.js:230
Traceback:
get_file/$target<@http://0.0.0.0:8069/web/static/src/js/framework/ajax.js:230:35
jQuery.event.dispatch@http://0.0.0.0:8069/web/static/lib/jquery/jquery.js:4640:50
jQuery.event.add/elemData.handle@http://0.0.0.0:8069/web/static/lib/jquery/jquery.js:4309:41


Back End Error :-

File "/usr/share/pyshared/werkzeug/serving.py", line 159, in run_wsgi

execute(app)

File "/usr/share/pyshared/werkzeug/serving.py", line 146, in execute

application_iter = app(environ, start_response)

File "/home/jigneshm/workspace/odoo/openerp/service/server.py", line 245, in app

return self.app(e, s)

File "/home/jigneshm/workspace/odoo/openerp/service/wsgi_server.py", line 184, in application

return application_unproxied(environ, start_response)

File "/home/jigneshm/workspace/odoo/openerp/service/wsgi_server.py", line 170, in application_unproxied

result = handler(environ, start_response)

File "/home/jigneshm/workspace/odoo/openerp/http.py", line 1422, in __call__

return self.dispatch(environ, start_response)

File "/home/jigneshm/workspace/odoo/openerp/http.py", line 1396, in __call__

return self.app(environ, start_wrapped)

File "/usr/share/pyshared/werkzeug/wsgi.py", line 411, in __call__

return self.app(environ, start_response)

File "/home/jigneshm/workspace/odoo/openerp/http.py", line 1586, in dispatch

result = ir_http._dispatch()

File "/home/jigneshm/workspace/odoo/addons/web_editor/models/ir_http.py", line 16, in _dispatch

return super(ir_http, self)._dispatch()

File "/home/jigneshm/workspace/odoo/openerp/addons/base/ir/ir_http.py", line 186, in _dispatch

return self._handle_exception(e)

File "/home/jigneshm/workspace/odoo/openerp/addons/base/ir/ir_http.py", line 157, in _handle_exception

return request._handle_exception(exception)

File "/home/jigneshm/workspace/odoo/openerp/http.py", line 743, in _handle_exception

return super(HttpRequest, self)._handle_exception(exception)

File "/home/jigneshm/workspace/odoo/openerp/addons/base/ir/ir_http.py", line 182, in _dispatch

result = request.dispatch()

File "/home/jigneshm/workspace/odoo/openerp/http.py", line 774, in dispatch

r = self._call_function(**self.params)

File "/home/jigneshm/workspace/odoo/openerp/http.py", line 316, in _call_function

return checked_call(self.db, *args, **kwargs)

File "/home/jigneshm/workspace/odoo/openerp/service/model.py", line 118, in wrapper

return f(dbname, *args, **kwargs)

File "/home/jigneshm/workspace/odoo/openerp/http.py", line 309, in checked_call

result = self.endpoint(*a, **kw)

File "/home/jigneshm/workspace/odoo/openerp/http.py", line 893, in __call__

return self.method(*args, **kw)

File "/home/jigneshm/workspace/odoo/openerp/http.py", line 471, in response_wrap

response = f(*args, **kw)

File "/home/jigneshm/key_concept_jc/web/controllers/main.py", line 1049, in content_common

response.set_cookie('fileToken', token)

AttributeError: 'NotFound' object has no attribute 'set_cookie'



Hope you understand my question.

Thanks in advance.

Avatar
Discard
3 Answers
2
Avatar
Jose Vasquez
Best Answer

Hi

I spent a bit of code that I use, I worked for odoo9

import xlwt

from xlsxwriter.workbook import Workbook

from cStringIO import StringIO

import base64


def print_excel_report(self, cr, uid, ids, context=None):

    filename= 'detalle_cobranzas.xls'

    workbook= xlwt.Workbook(encoding="UTF-8")

    worksheet= workbook.add_sheet('Detalle de cobranzas')

    style = xlwt.easyxf('font:height 400, bold True, name Arial; align: horiz center, vert center;borders: top medium,right medium,bottom medium,left medium')

    worksheet.write_merge(0,1,0,7,'REPORT IN EXCEL',style)

    fp = StringIO()

    workbook.save(fp)

    export_id = self.pool.get('excel.extended').create(cr, uid, {'excel_file': base64.encodestring(fp.getvalue()), 'file_name': filename}, context=context)

    fp.close()

    return{

        'view_mode': 'form',

'res_id': export_id,

'res_model': 'excel.extended',

'view_type': 'form',

'type': 'ir.actions.act_window',

'context': context,

'target': 'new',

    }


class inventory_excel_extended(models.Model):

_name= "excel.extended"

excel_file = fields.Binary('Dowload report Excel')

file_name = fields.Char('Excel File', size=64)

in xml

        <!-- Add button in your xml form -->
        <button name="print_excel_report" string="Print Excel"/>
        
        <!-- xml excel -->
        <record id="view_excel_form_extended" model="ir.ui.view">
<field name="name">excel.extended.form</field>
<field name="model">excel.extended</field>
<field name="arch" type="xml">
<form string="Excel Report file">
<group>
<field name="excel_file" readonly="1" filename="file_name"/>
<field name="file_name" invisible="1"/>
</group>
</form>
</field>
</record>
<record id="action_excel_form" model="ir.actions.act_window">
<field name="name">Reportes Excel</field>
<field name="view_id" ref="view_excel_form_extended"/>
<field name="view_type">form</field>
<field name="view_mode">form</field>
<field name="res_model">excel.extended</field>
<field name="target">new</field>
</record>

I hope you helps, Greetings.

3 Comments
Avatar
Discard
Avatar
Jignesh Mehta
-

Thank you so much Jose.

Avatar
Jignesh Mehta
-

How to add image in the xls report. It gives me error :- Exception: String longer than 32767 characters

Avatar
Jose Vasquez
-

Hi Jignesh, This could serve you. http://xlsxwriter.readthedocs.org/en/latest/page_setup.html

0
Avatar
Fernando
Best Answer

Hola Jose, use tu código, pero al pulsar el botón que creé no me genera el archivo xls. Podrías ayudarme con esa parte ?. Muchas gracias.

Avatar
Discard
0
Avatar
Anh Vu
Best Answer

Hi,

I prefer to use report_xls -> https://github.com/OCA/reporting-engine

Regards

1 Comment
Avatar
Discard
Avatar
Odoo Honduras
-

Any user manual to use it??