Hey Cyllo community! Anyone have any tips for creating dynamic XLSX reports in Cyllo? I'm struggling a bit. Thanks in advance!
Welcome!
Share and discuss the best content and new marketing ideas, build your professional profile and become a better marketer together.
This question has been flagged
Here's how you can build dynamic Excel reports in Odoo using Python and HTTP controllers. This approach offers granular control over report generation, which is particularly useful for complex reports and large datasets. Python Code – Abstract Model Logic ('models/example_report.py') This defines the report generation logic. from odoo import models import io import xlsxwriter class ExampleModelXlsx(models.AbstractModel): _name = 'report.my_module.example_model_xlsx' def generate_xlsx_report(self, workbook, data, records): sheet = workbook.add_worksheet('Example Model Report') bold = workbook.add_format({'bold': True}) row = 0 col = 0 sheet.write(row, col, 'Name', bold) sheet.write(row, col + 1, 'Description', bold) sheet.write(row, col + 2, 'Date', bold) row += 1 for record in records: sheet.write(row, col, record.name) sheet.write(row, col + 1, record.description or '') sheet.write(row, col + 2, str(record.date_created) or '') row += 1 This controller handles the HTTP request to generate and serve the XLSX file. from odoo import http from odoo.http import request, content_disposition import json import io import xlsxwriter class XLSXReportController(http.Controller): @http.route('/xlsx_report/example_model', type='http', auth='user', methods=['POST'], csrf=False) def download_xlsx_report(self, **kwargs): options = json.loads(kwargs.get('options', '{}')) model = 'example.model' records = request.env[model].search([]) output = io.BytesIO() workbook = xlsxwriter.Workbook(output, {'in_memory': True}) report_obj = request.env['report.my_module.example_model_xlsx'] report_obj.generate_xlsx_report(workbook, options, records) workbook.close() output.seek(0) filename = 'example_model_report.xlsx' headers = [ ('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'), ('Content-Disposition', content_disposition(filename)) ] return request.make_response(output.read(), headers) JavaScript – Trigger Report Download This JavaScript code handles the frontend interaction, triggering the report download. import { registry } from "@web/core/registry"; import { BlockUI } from "@web/core/ui/block_ui"; import { download } from "@web/core/network/download"; registry.category("ir.actions.report handlers").add("example_model_xlsx", async function (action) { if (action.report_type === 'xlsx') { const blockUI = new BlockUI(); await download({ url: '/xlsx_reports', //This URL needs to match your controller route data: action.data, complete: () => unblockUI, error: (error) => self.call('crash_manager', 'rpc_error', error), }); } }); ``` Remember to replace '/xlsx_reports' in the JavaScript with the actual route defined in your controller ('/xlsx_report/example_model' in this example). Also ensure that `example.model` reflects the actual name of your Odoo model. This provides a complete, functional example of dynamic Excel report generation in Odoo.