我使用report_xls模块创建了xls报告。现在我想在创建新记录时添加xls报告的附件。我使用的是odoo v8。
attachment_obj = self.pool.get('ir.attachment')
ir_actions_report = self.pool.get('ir.actions.report.xml')
matching_reports = ir_actions_report.search(cr, uid, [('report_name','=','timesheet.auto.invoice.xls')])
if matching_reports:
report = ir_actions_report.browse(cr, uid, matching_reports[0])
report_service = report.report_name
if report.report_type in ['qweb-html', 'qweb-pdf', 'xls']:
result, format = self.pool['report'].get_pdf(cr, uid, [x.id], report_service, context=context), 'xls'
else:
result, format = openerp.report.render_report(cr, uid, report_service, {'model': account_obj}, context)
result = base64.b64encode(report)
file_name = re.sub(r'[^a-zA-Z0-9_-]', '_', 'Timesheet Report')
file_name += ".xls"
attachment_id = attachment_obj.create(cr, uid,
{
'name': report.report_name,
'datas': base64.b64encode(report.report_name),
'report_name': 'timesheet.auto.invoice.xls',
'res_model': 'account.invoice',
'res_id': x.id,
'type': 'binary'
}, context)