Skip to Content
Meniu
Trebuie să fiți înregistrat pentru a interacționa cu comunitatea.
Această întrebare a fost marcată
5 Răspunsuri
15922 Vizualizări

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()
Imagine profil
Abandonează
Autor Cel mai bun răspuns

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.

Imagine profil
Abandonează

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

Cel mai bun răspuns

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.

Imagine profil
Abandonează
Cel mai bun răspuns

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.





Imagine profil
Abandonează
Cel mai bun răspuns

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

Imagine profil
Abandonează
Autor

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.

Related Posts Răspunsuri Vizualizări Activitate
2
apr. 24
22074
0
iun. 23
3002
0
oct. 20
3266
3
iun. 20
8961
0
apr. 16
6117