How to export xlsx data form my/order website portal
I added one button in sale order portal My Account -> Sales Orders page like this
<!-- Add the Export Button -->
<template id="portal_my_orders_inherit_export" inherit_id="sale.portal_my_orders" customize_show="True">
<xpath expr="//t[@t-call='portal.portal_searchbar']" position="before">
<button class="btn btn-primary mb-2 sale_export_btn_class" id="sale_export_btn_id" type="button">
Export Sale
</button>
</xpath>
</template>
When i click on this button export a xlsx file with all the sale order of the logged user
Please help me to resolve this.
Thank you.
The xlsx report its a controller you should call it from your button or create a widget that response to your onClick event button to call the controller and return the response to the browser with the report inside it.
I hope this answer can be helpful for you.
You can use a <a> tag and define a controller function to render and download the XLSX report.
In the following, we suppose you already created an XLSX report using the report_xlsx module
Example:
<a role="button" t-if="orders"
t-att-href="'/my/export_orders?order_ids=%s' % orders.ids"
class="btn btn-primary mb-2 mt-2"
target="_blank"
>
Alter the customer portal controller to define the /my/export_orders route
class CustomerPortalExtended(CustomerPortal):
@http.route('/my/export_orders', type='http', auth="user", website=True)
def export_orders(self, **kw):
order_ids = ast.literal_eval(kw['order_ids'])
report_sudo = request.env.ref(report_name).with_user(SUPERUSER_ID)
report = report_sudo._render_xlsx(order_ids, {})[0]
report_http_headers = [
('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'),
('Content-Length', len(report)),
('Content-Disposition', content_disposition("Orders.xlsx"))
]
return request.make_response(report, headers=report_http_headers)