在pivot view odoo中更改下载的xls文件的名称

在pivot view odoo中更改下载的xls文件的名称,pivot,odoo,xls,Pivot,Odoo,Xls,我想更改用户可以在销售点模块Stock Pivot view中下载的xls文件的名称。 单击pivot视图中的下载按钮,而不是“table.xls”,我希望它是例如“03-17-2020.xls” 但我不知道如何改变它 我尝试在这里或在odoo论坛中查找任何源代码或示例,但在您的控制器/部分中找不到任何尝试此代码 from collections import deque import json from odoo import http from odoo.http import reque

我想更改用户可以在销售点模块Stock Pivot view中下载的xls文件的名称。 单击pivot视图中的下载按钮,而不是“table.xls”,我希望它是例如“03-17-2020.xls” 但我不知道如何改变它
我尝试在这里或在odoo论坛中查找任何源代码或示例,但在您的控制器/部分中找不到任何

尝试此代码

from collections import deque
import json

from odoo import http
from odoo.http import request
from odoo.tools import ustr
from odoo.tools.misc import xlwt
from datetime import date
from odoo.addons.web.controllers.pivot import TableExporter  # Import the class


    class CustomTableExporter(TableExporter):# Inherit in your custom class

        @http.route('/web/pivot/export_xls', type='http', auth="user")
        def export_xls(self, data, token):
            jdata = json.loads(data)
            nbr_measures = jdata['nbr_measures']
            workbook = xlwt.Workbook()
            worksheet = workbook.add_sheet(jdata['title'])
            header_bold = xlwt.easyxf("font: bold on; pattern: pattern solid, fore_colour gray25;")
            header_plain = xlwt.easyxf("pattern: pattern solid, fore_colour gray25;")
            bold = xlwt.easyxf("font: bold on;")

            # Step 1: writing headers
            headers = jdata['headers']

            # x,y: current coordinates
            # carry: queue containing cell information when a cell has a >= 2 height
            #      and the drawing code needs to add empty cells below
            x, y, carry = 1, 0, deque()
            for i, header_row in enumerate(headers):
                worksheet.write(i, 0, '', header_plain)
                for header in header_row:
                    while (carry and carry[0]['x'] == x):
                        cell = carry.popleft()
                        for i in range(nbr_measures):
                            worksheet.write(y, x + i, '', header_plain)
                        if cell['height'] > 1:
                            carry.append({'x': x, 'height': cell['height'] - 1})
                        x = x + nbr_measures
                    style = header_plain if 'expanded' in header else header_bold
                    for i in range(header['width']):
                        worksheet.write(y, x + i, header['title'] if i == 0 else '', style)
                    if header['height'] > 1:
                        carry.append({'x': x, 'height': header['height'] - 1})
                    x = x + header['width']
                while (carry and carry[0]['x'] == x):
                    cell = carry.popleft()
                    for i in range(nbr_measures):
                        worksheet.write(y, x + i, '', header_plain)
                    if cell['height'] > 1:
                        carry.append({'x': x, 'height': cell['height'] - 1})
                    x = x + nbr_measures
                x, y = 1, y + 1

            # Step 2: measure row
            if nbr_measures > 1:
                worksheet.write(y, 0, '', header_plain)
                for measure in jdata['measure_row']:
                    style = header_bold if measure['is_bold'] else header_plain
                    worksheet.write(y, x, measure['measure'], style)
                    x = x + 1
                y = y + 1

            # Step 3: writing data
            x = 0
            for row in jdata['rows']:
                worksheet.write(y, x, row['indent'] * '     ' + ustr(row['title']), header_plain)
                for cell in row['values']:
                    x = x + 1
                    if cell.get('is_bold', False):
                        worksheet.write(y, x, cell['value'], bold)
                    else:
                        worksheet.write(y, x, cell['value'])
                x, y = 0, y + 1
            today = date.today()
            a = str(today) + '.xls'

            response = request.make_response(None,
                                             headers=[('Content-Type', 'application/vnd.ms-excel'),
                                                      ('Content-Disposition', 'attachment; filename=%s' % a)],
                                             cookies={'fileToken': token})
            workbook.save(response.stream)
            return response

这将以excel-2020-03-18.xls格式打印今天的日期名称

尝试在控制器/分区中输入此代码

from collections import deque
import json

from odoo import http
from odoo.http import request
from odoo.tools import ustr
from odoo.tools.misc import xlwt
from datetime import date
from odoo.addons.web.controllers.pivot import TableExporter  # Import the class


    class CustomTableExporter(TableExporter):# Inherit in your custom class

        @http.route('/web/pivot/export_xls', type='http', auth="user")
        def export_xls(self, data, token):
            jdata = json.loads(data)
            nbr_measures = jdata['nbr_measures']
            workbook = xlwt.Workbook()
            worksheet = workbook.add_sheet(jdata['title'])
            header_bold = xlwt.easyxf("font: bold on; pattern: pattern solid, fore_colour gray25;")
            header_plain = xlwt.easyxf("pattern: pattern solid, fore_colour gray25;")
            bold = xlwt.easyxf("font: bold on;")

            # Step 1: writing headers
            headers = jdata['headers']

            # x,y: current coordinates
            # carry: queue containing cell information when a cell has a >= 2 height
            #      and the drawing code needs to add empty cells below
            x, y, carry = 1, 0, deque()
            for i, header_row in enumerate(headers):
                worksheet.write(i, 0, '', header_plain)
                for header in header_row:
                    while (carry and carry[0]['x'] == x):
                        cell = carry.popleft()
                        for i in range(nbr_measures):
                            worksheet.write(y, x + i, '', header_plain)
                        if cell['height'] > 1:
                            carry.append({'x': x, 'height': cell['height'] - 1})
                        x = x + nbr_measures
                    style = header_plain if 'expanded' in header else header_bold
                    for i in range(header['width']):
                        worksheet.write(y, x + i, header['title'] if i == 0 else '', style)
                    if header['height'] > 1:
                        carry.append({'x': x, 'height': header['height'] - 1})
                    x = x + header['width']
                while (carry and carry[0]['x'] == x):
                    cell = carry.popleft()
                    for i in range(nbr_measures):
                        worksheet.write(y, x + i, '', header_plain)
                    if cell['height'] > 1:
                        carry.append({'x': x, 'height': cell['height'] - 1})
                    x = x + nbr_measures
                x, y = 1, y + 1

            # Step 2: measure row
            if nbr_measures > 1:
                worksheet.write(y, 0, '', header_plain)
                for measure in jdata['measure_row']:
                    style = header_bold if measure['is_bold'] else header_plain
                    worksheet.write(y, x, measure['measure'], style)
                    x = x + 1
                y = y + 1

            # Step 3: writing data
            x = 0
            for row in jdata['rows']:
                worksheet.write(y, x, row['indent'] * '     ' + ustr(row['title']), header_plain)
                for cell in row['values']:
                    x = x + 1
                    if cell.get('is_bold', False):
                        worksheet.write(y, x, cell['value'], bold)
                    else:
                        worksheet.write(y, x, cell['value'])
                x, y = 0, y + 1
            today = date.today()
            a = str(today) + '.xls'

            response = request.make_response(None,
                                             headers=[('Content-Type', 'application/vnd.ms-excel'),
                                                      ('Content-Disposition', 'attachment; filename=%s' % a)],
                                             cookies={'fileToken': token})
            workbook.save(response.stream)
            return response

今天将打印日期名称为excel-2020-03-18.xls

谢谢分享这个答案你真的救了我,谢谢谢谢分享这个答案你真的救了我,谢谢