XLS reports are often necessary for many business organizations. Data representation and manipulation can be done in an XLS file very easily. In this blog, we will be discussing ‘how to create XLs reports in Odoo.’ As you know Odoo doesn’t’ support XLS file creation of a report by default. So you need an extra module ‘report_xls’ for this purpose. You can download it from here: XLSX Report
Here, we are going to create an XLS report from a wizard. Wizard can be used to enter the data. It is based on this data that we can filter the report. In the wizard, we will add some fields to filter the content of the report. So let us first look how the wizard is created.
Wizard Creation
Python file:
class
CrmReport(models.TransientModel):
_name = 'crm.won.lost.report'
sales_person = fields.Many2one('res.users', string="Sales Person")
start_date = fields.Date('Start Date')
end_date = fields.Date('End Date',default=fields.Date.today)
def print_xls_report(self,cr,uid,ids,context=None):
data= self.read(cr, uid, ids)[0]
return {'type': 'ir.actions.report.xml',
'report_name': 'crm_won_lost_report.report_crm_won_lost_report.xlsx',
'datas': data
}
This is the python file created for the wizard. Here the fields start_date, end_date, and sales_person are filtration fields. The data in the wizard can be filtered according to the value in the fields
XML File
Wizard view:
<record id="view_crm_won_lost_report" model="ir.ui.view">
<field name="name">CRM Report</field>
<field name="model">crm.won.lost.report</field>
<field name="arch" type="xml">
<form string="Choose your details">
<group>
<group>
<field name="sales_person" style="width: 40%%" />
</group>
<group>
<field name="start_date" />
<field name="end_date" />
</group>
</group>
<footer>
<button name="print_xls_report" string="Print Report" type="object" class="oe_highlight" />
or
<button string="Cancel" class="oe_link" special="cancel" groups="base.group_sale_manager"/>
</footer>
</form>
</field>
</record>
This is the code for the view of the wizard (or the design of the wizard). In this view, the user can enter the filter details such as date and the salesperson. After selecting the required fields, the user can click the ‘print report’ button to print the ‘Xls’ report. When the user clicks the print button, the call goes to ‘print_xls_report’ function defined in the given model
defprint_xls_report(self,cr, uid,ids, context=None): rec = self.browse(data)data = {}data['form'] = rec.read(['sales_person', 'start_date', 'end_date'])return self.env['report'].get_action(rec, 'crm_won_lost_report.report_crm_won_lost_report.xlsx',data=data
In the next step, we have to define the report in the name ‘report_crm_won_lost_report’
from
openerp.addons.report_xlsx.report.report_xlsx
import
ReportXlsx
class
CrmReportWonLost
(ReportXlsx):
def
generate_xlsx_report(self,workbook,data,lines):
#We can recieve the data entered in the wizard here as data
sheet
= workbook.add_worksheet()
CrmReportWonLost('report.crm_won_lost_report.report_crm_won_lost_report.xlsx','crm.won.lost.report')
In the above code, we have to write the last line properly. It should be like this
Classname('report.module_name.report_name.xlsx', 'model_name')
Now we can load the report to the database. For that, we have to add a report tag in XML,
<report id="report_crm_xlsx"
model="crm.won.lost.report"
string="Won/LostReport"
report_type="xlsx"
name="crm_won_lost_report.report_crm_won_lost_report.xlsx"
file="crm_won_lost_report.report_crm_won_lost_report.xlsx"
attachment_use="False"/>
This is developed in version 9. You can follow similar steps in v8 and in v10. According to the version, we have to change the version of the ‘report_xlsx’. It can be downloaded from Odoo apps.
We already have uploaded a module using the above code in the app store, you can check and use it for reference along with this blog.