1.首先按钮或者菜单出发一个后端方法返回url动作
def download_xxx_excel(self): url = '/export_xxx_excel?id={self.id} return { 'type': 'ir.actions.act_url', 'url': url, 'target': 'new', 'tfs_close': True }
2.controller
from odoo import http import xlsxwriter, io import base64 @http.route(['/export_xxx_excel'], type='http', auth="user", website=True) def export_xxx_excel(self, **kw): id = kw.get('id') output_buffer = io.BytesIO() workbook = xlsxwriter.Workbook(output_buffer, options={'default_format_properties': {'font_name': 'Microsoft Yahei', 'font_size': 9, 'valign': 'vcenter'}}) format_title = workbook.add_format({'font_size': 20, 'align': 'center'}) format_header = workbook.add_format({'font_size': 10, 'border': True, 'bold': True, 'align': 'center'}) format_default = workbook.add_format({'text_wrap': True, 'border': True, 'border_color': 'gray'}) format_left = workbook.add_format({'font_size': 10, 'border': True, 'bold': True, 'align': 'left'}) worksheet = workbook.add_worksheet("worksheet_name") worksheet.write("A1", "Python") worksheet.write("B1", "Excel") worksheet.write("C1", "xlsx") workbook.close() output_buffer.seek(0) byte_data = output_buffer.getvalue() # 在内存中读取 base64_data = base64.b64encode(byte_data) # 转为base64 file_name = "xxx.xlsx" byte_data = base64.b64decode(base64_data) response = http.request.make_response( None, headers=[ ('Content-Type', 'application/vnd.ms-excel'), ('Content-Disposition', http.content_disposition(file_name)) ] ) response.stream.write(byte_data) return response
标签:自定义,format,excel,base64,Odoo12,worksheet,workbook,True From: https://www.cnblogs.com/wangdianchao/p/17622799.html