Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
4 Trả lời
6737 Lượt xem

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 *****


Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

Hi,

Have a look at this blog: https://www.cybrosys.com/blog/how-to-create-a-xls-report-in-odoo


Thanks

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất
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


Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

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.

Ảnh đại diện
Huỷ bỏ
Tác giả

And what should i return ?

Bài viết liên quan Trả lời Lượt xem Hoạt động
3
thg 7 25
26845
2
thg 12 17
3473
0
thg 12 21
3394
5
thg 12 19
11055
4
thg 10 18
4743