This question has been flagged
5 Replies
13695 Views

Hello, I associated with a button a method that allows the generation of an xlsx file, I would like to return the file just created, as for a normal file, with the Save as...

This is my code, I do not know how to write the return, I need help.

   @api.multi
    def export(self):
        workbook = xlsxwriter.Workbook('/tmp/test.xlsx')
        worksheet = workbook.add_worksheet()
        row = 0
        col = 0
        for vals in self.carrier_line_ids:
            worksheet.write(row, 0, vals.reference)
            ...
            ...
            row += 1


        workbook.close()
Avatar
Discard
Author Best Answer

My solution:

@api.multi
    def export(self):
        file_name = 'temp'
        workbook = xlsxwriter.Workbook(file_name, {'in_memory': True})
        worksheet = workbook.add_worksheet()
        row = 0
        col = 0
        for e in header:
            worksheet.write(row, col, e)
            col += 1
        row += 1
        for vals in self.carrier_line_ids:
            worksheet.write(row, 0, vals.reference)
            ...
            ...
        workbook.close()
        with open(file_name, "rb") as file:
            file_base64 = base64.b64encode(file.read())
         self.carrier_xlsx_document_name = self.name + '.xlsx'
        self.write({'carrier_xlsx_document': file_base64, })

carrier_xlsx_document is a binary


view.xml

<field name="carrier_xlsx_document" widget="binary" filename="carrier_xlsx_document_name"
attrs="{'invisible':[('state','=', 'draft')]}" readonly="1"/>
<field name="carrier_xlsx_document_name" invisible="1"/>

Add to import xlsxwriter and base64

I hope this solution can help other programmers.

A request for mods: you can change the name of the thread and add SOLVED, I can't.

Avatar
Discard

Hi, I am trying this solution but I get the following error message:
FileNotFoundError: [Errno 2] No such file or directory: 'Registro_Ventas_12/2021.xlsx'
Do you know why is this happening?
This is my code:
def write_xlsx(self, file_name, header_list, field_list):
workbook = xlsxwriter.Workbook(file_name, {'in_memory': True})
worksheet = workbook.add_worksheet()
row = col = 0
# Write header row
for header in header_list:
worksheet.write(row, col, header)
col += 1
row += 1
# Write value rows
n = 1
for line in self.lcv_line_ids:
line_registry = self.env['lcv.line'].browse([line.id])
worksheet.write(row, 0, n)
col = 1
for field in field_list:
if isinstance(getattr(line_registry, field), float):
value = str(round(getattr(line_registry, field), 2))
else:
value = str(getattr(line_registry, field))
worksheet.write(row, col, value)
col += 1
n += 1
row += 1
workbook.close()
with open(file_name, "wb") as file:
file_base64 = base64.b64encode(file.read())
return file_base64

Best Answer

I have no clue how to help you, but the forum software won't let me ask my question until I've answered other people's questions, so I'm going to go with a classic answer: paint ram's blood over your doorway.

Avatar
Discard
Best Answer

Hi Paolo,

Go to,

Setting>>Translation>>Import/ Export>>Export Translation

The popup wizard generate the translation file and allow you to download the file.

This will give you an idea how you can make it to work with your module.

You will find the reference code inside base mdoule

I hope this will give you an idea, how you can hit the goal.

Rgds,

Anil.





Avatar
Discard
Best Answer

Hi,

We have a written a blog about how to create XLS report in odoo 10, you can go through it: How to Create an XLS Report in Odoo ?


Thanks

Avatar
Discard
Author

This does not help me, I create an xlsx file and I want it to be saved, for now I create it but I can not generate the window with a direct link to the file.