This question has been flagged
1 Reply
2601 Views

I placed an excel template edited in advance. When the excel template is downloaded, the user's name and time can be used as the name of Excel

Avatar
Discard
Best Answer

Hi 

Try the following code,

def action_print_product_move(self):
   
""" Here generate a dictionary of a list of data that return to
            report action. And it will generate the xlsx report. """

    data = {
       
'location': self.location_id.complete_name,
       
'date': self.date,
       
'user': self.env.user.name,

// This is for example in this way you can add your own datas


    }
   
return {
       
'type': 'ir.actions.report',
       
'data': {'model': 'stock.move.location.report',
               
'options': json.dumps(data,
                                      default=date_utils.json_default),
               
'output_format': 'xlsx',
               
'report_name': user+date ,

                 // You can add your won value in the report name

                },
       
'report_type': 'xlsx',
    }

Controller File:

def get_report_xlsx(self, model, options, output_format, report_name, **kw):
   
""" Controller function for generating the xlsx report."""
    uid = request.session.uid
    report_obj = request.env[model].with_user(uid)
    options = json.loads(options)
    token =
'dummy-because-api-expects-one'
   
try:
       
if output_format == 'xlsx':
            response = request.make_response(
               
None,
                headers=[
                    (
'Content-Type', 'application/vnd.ms-excel'),
                    (
'Content-Disposition',
                    content_disposition(report_name +
'.xlsx'))
                ]
            )
            report_obj.get_report_xlsx(options, response)
        response.set_cookie(
'fileToken', token)
       
return response
   
except Exception as e:
        error = {
           
'code': 200,
           
'message': 'Odoo Server Error',
           
'data': http.serialize_exception(e)
        }
   
return request.make_response(html_escape(json.dumps(error)))
   
   
   

Hope it helps

Avatar
Discard