1

我正在尝试使用 Odoo 10 生成 excel 报告。代码运行良好,我能够做到。但是文件不会一键下载。它保存文件并在向导上显示下载链接。但我不想要这个额外的步骤。我希望通过单击下载文件。我在这里分享我的工作代码。请看一下并建议我应该添加什么以使其只需单击即可工作。

xml代码:

            <div state="get">

                <group>

                    <field name="name" colspan="4" invisible="1"/>

                    <field name="report" filename="name" colspan="4"/>

                </group>

            </div>

            <button name="generate_xls_report" string="Export XLS" type="object" class="oe_highlight" />

蟒蛇代码:

from odoo import fields, models, api, _

from odoo.tools import DEFAULT_SERVER_DATE_FORMAT
import xlwt
import base64
import cStringIO
from datetime import datetime


class CasesWizard(models.TransientModel):
    _name = "cases.wizard"
    _description = "Cases wizard"

    case_id = fields.Many2one('project.project', string='Cases')
    event_id = fields.Many2one('calendar.event', string='Events')
    company_id = fields.Many2one('res.company', string='company id', readonly=True,default=lambda self: self.env.user.company_id.id)
    lawyer_id = fields.Many2one('res.users', string='Lawyers')
    #partner_id = fields.Many2one('res.partner', string='Clients')
    date_from = fields.Date(string='Start Date')
    date_to = fields.Date(string='End Date')


    state = fields.Selection([('choose', 'choose'), ('get', 'get')],default='choose')
    report = fields.Binary('Prepared file', filters='.xls', readonly=True)
    name =  fields.Char('File Name', size=32)
    @api.multi
    def generate_xls_report(self):

        self.ensure_one()

        wb1 = xlwt.Workbook(encoding='utf-8')
        ws1 = wb1.add_sheet('Case Event Details')
        fp = cStringIO.StringIO()


# Here all excel data and calculations


        wb1.save(fp)
        out = base64.encodestring(fp.getvalue())
        self.write({'state': 'get', 'report': out, 'name':'event_details.xls'})

        return {
            'type': 'ir.actions.act_window',
            'res_model': 'cases.wizard',
            'view_mode': 'form',
            'view_type': 'form',
            'res_id': self.id,
            'views': [(False, 'form')],
            'target': 'new',
            'name': 'Event Details Report'
        }
4

1 回答 1

0

您可以使用web controller.
将以下方法添加到向导模型中:

@api.multi
def generate_xls_report(self):
    self.ensure_one()
    return {
        'type': 'ir.actions.act_url',
        'url': '/web/binary/download_xls_document?model=cases.wizard&id=%s&filename=event_details.xls' % (
            self.id),
        'target': 'new',
    }

然后创建 Web 控制器:

class Binary(http.Controller):

@http.route('/web/binary/download_xls_document', type='http', auth="public")
@serialize_exception
def download_xls_document(self, model, id, filename=None, **kw):
    Model = request.registry[model]
    cr, uid, context = request.cr, request.uid, request.context

    wb1 = xlwt.Workbook(encoding='utf-8')
    ws1 = wb1.add_sheet('Case Event Details')
    fp = cStringIO.StringIO()

    # Here all excel data and calculations

    wb1.save(fp)
    filecontent = fp.getvalue()

    if not filecontent:

        return request.not_found()

    else:
        if not filename:
            filename = '%s_%s' % (model.replace('.', '_'), id)
        return request.make_response(filecontent,
                                     [('Content-Type', 'application/octet-stream'),
                                      ('Content-Disposition', content_disposition(filename))])

这应该会生成一个空的 xls 文件。

于 2018-06-11T10:01:09.550 回答