This question has been flagged
4 Replies
5246 Views

Hello,

I'm working on Odoo 11, and i have a button to print XLS Report, I generate the report successfully using the xlswriter library but i don't know what should i return in the function in order to allow the user to download the XLS file.

Please, see the code bellow and tell me what should i put instead of *****?

def generate_excel_report(self):
self.calculate_validation_lines()
workbook = xlsxwriter.Workbook("Stock valuation report {0} to {1}".format(str(self.date_from), str(self.date_to)))
for company in self.all_companies:
if self.has_valuation(company):
sheet = self.create_worksheet(workbook, company)
row = 9
col = 0
for line in self.stock_valuation_lines:
if line.product_id.company_id == company:
sheet.write(row, col, line.product_id.categ_id.name)
sheet.write(row, col+1, line.product_id.name)
sheet.write(row, col+2, line.product_id.barcode)
sheet.write(row, col+3, line.product_id.default_code)
sheet.write(row, col+4, line.beginning_qty)
sheet.write(row, col+5, line.received_qty)
sheet.write(row, col+6, line.sale_qty)
sheet.write(row, col+7, line.internal_qty)
sheet.write(row, col+8, line.adjustment_qty)
sheet.write(row, col+9, line.ending_qty)
sheet.write(row, col+10, line.cost)
sheet.write(row, col+11, line.total_value)
workbook.close()
return *****


Avatar
Discard
Best Answer
xlsx_data = output.getvalue()    
return request.make_response(xlsx_data,
[('Content-Type', 'application/vnd.ms-excel'),
('Content-Disposition', content_disposition(str(section_name)+'.xlsx'))])

Hope this will heps you


Avatar
Discard
Best Answer

First save, then close

    workbook.save()
workbook.close()

That will save the file.  By the looks of it, you didn't specify a file path so it will be in whatever directory the Python script is in.  I would use `tempfile` to store into a temporary directory.  If you need the file path, you can then return that.  If not, you can use open in read-binary & return the file in binary.

Avatar
Discard
Author

And what should i return ?