How To Generate XLSX Report Using Controller in Odoo 18

December 16, 2024 by
admin


                                                                                                                 



How To Generate XLSX Report Using Controller in Odoo 18

Overview
The ability to generate dynamic reports in Excel (XLSX) format is a crucial feature in enterprise resource planning (ERP) systems like Odoo. These reports offer users the flexibility to analyze, share, and archive structured data in a portable and editable format.
This document outlines two possible solutions for creating XLSX reports in Odoo without using the report_xlsx module. For that we utilize Odoo's HTTP controller to achieve a modular and scalable solution.
https://odoo-community.org/shop/base-report-xlsx-2202




  • Using Controller and Transient Model
- Add report content in wizard model
- Add new transient model 'common.xlsx.out' with filedata, filename fields.
- Add Common controller to download file


  • zb_outstanding_report / views /outstanding_report_wiz_view.xml


  
    button name="action_outstanding_report_xlsx" string="XLSX" type="object"                   
    class="btn-primary"/> 



  • zb_outstanding_report / wizard /outstanding_report_wiz.py


    
from odoo import models, fields 
import io 
import base64 
import xlsxwriter 

def action_outstanding_report_xlsx(self):
    for obj in self:
        data = self.read()[0]
        output = io.BytesIO()
        workbook = xlsxwriter.Workbook(output, {'in_memory': True})
        sheet = workbook.add_worksheet('Partner Ledger')
        workbook.close()
        output.seek(0)
        result = base64.b64encode(output.read())
        report_id = self.env['common.xlsx.out'].sudo().create({
            'filedata': result,
            'filename': 'Partner Ledger.xls'
        })

        return {
            'type': 'ir.actions.act_url',
             'url':   '/web/binary/download_document?model=common.xlsx.out&field=filedata&id=%s&filename=%s.xls' % (report_id.id, 'Partner Ledger.xls'),
             'target': 'new',
           }
           output.close()



  • zb_outstanding_report /wizard /common_xlsx_out.py


    
 from odoo import models,fields
 
 class CommonXlsxOut(models.TransientModel):
    _name = 'common.xlsx.out'

    filedata = fields.Binary('Download File', readonly=True)
    filename = fields.Char('Filename', readonly=True)



  • zb_outstanding_report /controllers / main.py


    
from odoo import http
from odoo.http import request, content_disposition
import base64

class Binary(http.Controller):

    @http.route('/web/binary/download_document', type='http', auth="public")
    def download_document(self, model, field, id, filename=None, **kw):
        record = request.env[model].sudo().browse(int(id))
        filecontent = base64.b64decode(record[field] or '')
        if not filename:
            filename = f"{model.replace('.', '_')}_{id}"
        if not filecontent:
            return request.not_found()
        return request.make_response(
            filecontent,
            [
                ('Content-Type', 'application/octet-stream'),
                ('Content-Disposition', content_disposition(filename)),
            ],
        )



Clicking on the Outstanding Statement menu will display the pop-up defined in the wizard



The XLSX report will be generated upon clicking the XLSX button



Conclusion
The implementation of XLSX report generation in Odoo provides developers with flexible and efficient solutions to meet various reporting needs. Whether choosing the wizard-based approach with a common controller or the direct controller approach each method offers distinct advantages that can be leveraged based on specific requirements. Common controller provides better modularity and maintainability for complex reports, while direct controller offers a streamlined solution for simpler reporting needs. 


If you are looking for an ERP implementation partner with diverse industry experience feel free to contact us. We have proven track record of successful implementations across various sectors including Odoo for Manufacturing, Odoo for Trading, Odoo for FMCG, Odoo for Oil & Gas, Odoo for Diary, Odoo for Pharma, Odoo for Cosmetic Clinic, Odoo for Contracting Companies, Odoo for HVAC, Odoo for Logistics, Odoo for Automobile, Odoo for Laundry, Odoo for Field Service, Odoo for E-Commerce & many more


ZestyBeanz offers Developer / Consultant outsourcing programs, Chat with us in Whatsapp and Hire Odoo Developers, Mobile Application Developers, Consultants.
#OdooKerala #OdooKochi #OdooTrivandrum #OdooERP #ProjectManagement #OdooVansales #HireOdooDeveloper
in R&D
# R&D