Skip to Content
Menu
This question has been flagged
7 Replies
13936 Views


Hello,

I want to generate excel report on button click, can any one help me out how this can be done. I'm using odoo 12

Thanks 


Avatar
Discard
Best Answer

Hello Hoang Quan,


Please find below code for the excel report of particular product. Add button in Product form view with method name "product_template_excel_report".

PY File :-
==========

from odoo import models, fields, api, _
from odoo.exceptions import UserError
from datetime import date, timedelta, datetime
import datetime
from odoo.tools import DEFAULT_SERVER_DATE_FORMAT
import tempfile
from odoo.tools.misc import xlwt
import io
import base64
import time
from dateutil.relativedelta import relativedelta
from pytz import timezone

class ProductTemplate(models.TransientModel):
    _inherit = 'product.template'

    @api.multi
    def product_template_excel_report(self):
        filename= 'Product Report ' + str(self.name) + '.xls'
        workbook= xlwt.Workbook()

        worksheet= workbook.add_sheet('Product Report')
        font = xlwt.Font()
        font.bold = True
        for_left = xlwt.easyxf("font: bold 1, color black; borders: top double, bottom double, left double, right double; align: horiz left")
        for_left_not_bold = xlwt.easyxf("font: color black; align: horiz left")
        for_center_bold = xlwt.easyxf("font: bold 1, color black; align: horiz center")
        GREEN_TABLE_HEADER = xlwt.easyxf(
            'font: bold 1, name Tahoma, height 250;'
            'align: vertical center, horizontal center, wrap on;'
            'borders: top double, bottom double, left double, right double;'
            )
        style = xlwt.easyxf('font:height 400, bold True, name Arial; align: horiz center, vert center;borders: top medium,right medium,bottom medium,left medium')

        alignment = xlwt.Alignment()  # Create Alignment
        alignment.horz = xlwt.Alignment.HORZ_RIGHT
        style = xlwt.easyxf('align: wrap yes')
        style.num_format_str = '0.00'

         worksheet.row(0).height = 320
        worksheet.col(0).width = 4000
        worksheet.col(1).width = 4000
        borders = xlwt.Borders()
        borders.bottom = xlwt.Borders.MEDIUM
        border_style = xlwt.XFStyle()  # Create Style
        border_style.borders = borders

        product_title = 'Product Report ' + str(self.name)
        worksheet.write_merge(0,1,0,2,product_title,GREEN_TABLE_HEADER)

        row = 2

         worksheet.write(row, 0, 'Product Name' or '',for_left)
        worksheet.write(row, 1, 'Sales Price' or '',for_left)

        row = row + 1
        worksheet.write(row, 0, self.name or '',for_left_not_bold)
        worksheet.write(row, 1, self.list_price or '',for_left_not_bold)

        fp = io.BytesIO()
        workbook.save(fp)
        product_id = self.env['product.excel.extended'].create({'excel_file': base64.encodestring(fp.getvalue()), 'file_name': filename})
        fp.close()

        return{
            'view_mode': 'form',
            'res_id': product_id.id,
            'res_model': 'product.excel.extended',
            'view_type': 'form',
            'type': 'ir.actions.act_window',
             'context': self._context,
             'target': 'new',
         }


class ProductExcelExtended(models.Model):
        _name = 'product.excel.extended'
        _description = "Product Excel Extended"

         excel_file = fields.Binary('Download Report :- ')
         file_name = fields.Char('Excel File', size=64)



XML File :-
===========

<?xml version="1.0" encoding="utf-8"?>
<odoo>

     <!-- Excel Report Download Wizard Form View-->
     <record id="view_product_excel_form_extended" model="ir.ui.view">
     <field name="name">product.excel.extended.form</field>
     <field name="model">product.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>
         <footer>
             <button string="Cancel" class="oe_link oe_highlight" special="cancel"/>
        </footer>
         </form>
         </field>
    </record>

     <!-- Excel Report Download Wizard Action View-->
     <record id="action_product_excel_form" model="ir.actions.act_window">
         <field name="name">Reports Excel</field>
         <field name="view_id" ref="view_product_excel_form_extended"/>
         <field name="view_type">form</field>
         <field name="view_mode">form</field>
         <field name="res_model">product.excel.extended</field>
         <field name="target">new</field>
     </record>

</odoo>

CSV File :-
===========

id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
access_product_excel_extended,access.product.excel.extended,model_product_excel_extended,,1,1,1,1


Hope it will helps for you.

Thanks,

Avatar
Discard
Author

thank you. i added your code. But in view file excel not display. I can't download it :( how to download ?

Please help me. Thanks

This URL will download a file if it is generated for the record, you have to set filename and record id of product.excel.extended model

localhost:8069/web/content/?model=product.excel.extended&download=true&field=excel_file&id=Enter Recordid&filename=Enterexcelfilename

Best Answer

Hi,

If you are looking how to generate excel report in odoo, see this: How To Create Excel/XLS Report in Odoo

Thanks

Avatar
Discard
Author

it's not for me. I need get xls for tree view. Can u help me ?

Best Answer

Hi,

   You can just refer the Odoo Apps store for any example modules. here is one .please check it and make necessary changes   XLSX REPORT SAMPLE     

Avatar
Discard
Author Best Answer

[img]https://i.imgur.com/1YFNIwd.png[/img]


Hello Jignesh Mehta
I added your code. But it's nothing. Help me pls. Thanks

Avatar
Discard

Have you add access rights from csv file?

Author

yes i do?. All fields already exist in Database. U can see my image https://i.imgur.com/1YFNIwd.png

Related Posts Replies Views Activity
1
Nov 22
1505
0
Feb 17
2301
0
Mar 15
3098
0
Mar 15
4364
2
Sep 23
410