Blog

News & Events, Quick Tips
Creating Excel Report Using Odoo
March 22, 2022
The reports will provide important details to help develop future forecasts marketing plans, guide budget planning, and improve decision-making. Besides tracking progress and growth, managers also use business reports for identifying trends or any irregularities that may require further investigation. For all organizations, Excel reports play a very important role.
In Excel, we can do very easy data manipulation and representation. In Odoo, you can easily custom your own excel report. As you know that by default, Odoo does not support XLS files. So by using the ‘report_xls’ module, you can download it.
Here, we can create an excel report from a wizard.
Wizard Creation
A wizard is a transient model in Odoo. The fields in the wizards are used to filter the content in the report.Python file
from odoo import API, fields, modelsclass WizardClassName(models.TransientModel): _name = 'wizard.wizard' _description = 'XLS Report' date = field.Date(string=”Date”)
XML File
Also, create a .xml file for the wizard to display on the user interface<record id="view_wizard_report" model="ir.ui.view"> <field name="name">Wizard sample</field> <field name="model">wizard.wizard</field> <field name="arch" type="xml"> <form string="Choose your details"> <group> <group> <field name="start_date" /> </group> </group> <footer> <button name="export_xls_report" string="Export" type="object" class="oe_highlight" /> or <button string="Cancel" class="oe_link" special="cancel"/> </footer> </form> </field> </record>You can specify the action and menu item of the wizard in this XML file. You can write a method in the python file to fetch the data and render the excel report id. Creating an action for a report is referred to as a report action. The model will recognize the report action ‘ir.actions.report’ used in the record. Create a report action in your view file as shown below.
<record id="action_report_wizard_id" model="ir.actions.report"> <field name="name"><Wizard Sample<field> <field name="model">wizard.wizard</field> <field name="report_type">xlsx</field> <field name="report_name">report_wizard_id</field> <field name="report_file">module_name.report_wizard_id</field> <field name="print_report_name">Wizard Sample</field> </record>Here you can specify the name of the report, the model where you want to fetch data for your excel report and the report type, and the id of the main report function. You can specify the main method in the python file. Give an example below. from odoo import models
class ReportExcel(models.AbstractModel): _name = "report.module_name.report_wizard_id" _inherit = "report.report_xlsx.abstract" def generate_xlsx_report(self, workbook, data, lines): ws = workbook.add_worksheet("Report") // Add a name for your workbook ws.set_column(“A:A”,10) ws.set_column(“B:B”,10) //Set the column size sample_heading_format = ( { “bold”:True, “font_size”:20, “align”:center, “border”:True, } ) sample_format = ( { “bold”:True, “font_size”:16, “align”:center, “border”:True, } )// You can set the formats here and use them in your worksheet. ws.merge_range(“A1:A13”,”Sample Excel Report”,sample_heading_format) // By using merge_range function you can merge your columns/rows. ws.write(“B1:B13”,”This is Sample Excel Report”,sample_format) // Write function is used to write the values to your excel. You can easily customize your excel report according to your need in odoo. The module ‘report_xlsx’ version also changes depending on your odoo version.