0
votes

I have the method below that creates an Excel file, but how can I return it like an Excel report?

def report_excel(self,results,report_name,header,indice,title_report):
   fileExcel = xlsxwriter.Workbook('C:\\Users\\Pc-Pc\\Desktop\\411\\'+report_name+'.xlsx')
   listUsersSheet = fileExcel.add_worksheet(report_name)
   column = 0
   row = 15
   for res in results:
       listUsersSheet.merge_range(index[ind][0] + str(row+1) + ':'+ index[ind][0] + str(row + a), res[index[ind][1]], cell_format)
   fileExcel.close()

How can I download it from the client as a report?

1

1 Answers

0
votes

You can use a Web Controller for that:

from openerp.http import request
from openerp import http
from openerp.addons.web.controllers.main import serialize_exception,content_disposition


class Binary(http.Controller):
    @http.route('/web/binary/download_report', type='http', auth="public")
    @serialize_exception
    def download_xls_document(self, path, filename="My report.xlsx", **kw):
        with open(path, "rb") as pdf_file:
            return request.make_response(pdf_file.read(),
                                     [('Content-Type', 'application/octet-stream'),
                                      ('Content-Disposition', content_disposition(filename))])

and report_excel method should return:

return {
        'type': 'ir.actions.act_url',
        'url': '/web/binary/download_report?path=%s&filename=%s' % (path, "The report name.xlsx"),
        'target': 'blank',
    }