如何在ODOO上用excel (xls, xlsx)格式生成报告?

时间:2022-01-03 10:31:05

In purchase module, I want to generate the details which is being printed in the purchase order button as a pdf report. My requirement is how to print the report in excel format in odoo 10?

在购买模块中,我想生成将打印在购买订单按钮中的详细信息作为pdf报告。我的要求是如何在odoo 10中以excel格式打印报告?

2 个解决方案

#1


3  

In python:

在python中:

@api.multi
def method_name(self):
    dict={}
    list=[]

    dict['key1']=self.field_name
    dict['key2']=self.field_name
    dict['key3']=self.field_name

    for line in self.field_name:
        dict_1={}
        dict_1['key_sub1']=line.field_name,
        dict_1['key_sub2']=line.field_name,
        dict_1['key_sub3']=line.field_name,
        dict_1['key_sub4']=line.field_name,
        list.append(list)

    dict['key8']=dict_1  

    style0 = xlwt.easyxf('font: name Times New Roman, color-index blue, bold on')
    style1 = xlwt.easyxf('font: name Times New Roman, color-index red, bold on')
    style2 = xlwt.easyxf('font: name Times New Roman, color-index green, bold on')
    style3 = xlwt.easyxf('font: name Times New Roman, color-index black')
    style4 = xlwt.easyxf('font: name Times New Roman, color-index black, bold on')

    wb = xlwt.Workbook()
    ws = wb.add_sheet('A Excel  report')

    ws.write(0, 2, 'Header Name1',  style0)
    ws.write(0, 3, dict['key1'] , style3)
    ws.write(1, 2, 'Header Name2',  style1)
    ws.write(1, 3, dict['key2'] , style3)
    ws.write(2, 2, 'Header Name3',  style2)
    ws.write(2, 3, dict['key3'] , style3)

    ws.write(3, 1, 'Header Name4',  style4)
    ws.write(3, 2, 'Header Name5',  style4)
    ws.write(3, 3, 'Header Name6',  style4)
    ws.write(3, 4, 'Header Name7',  style4)
    n=4
    for vals in dict['key4']:
        ws.write(n, 1, vals['key_sub1'][0] , style3)
        ws.write(n, 2, vals['key_sub2'][0] , style3)
        ws.write(n, 3, vals['key_sub3'][0] , style3)
        ws.write(n, 4, vals['key_sub4'][0] , style3)
        n+=1

    current_date=datetime.now()
    date =current_date.strftime('%d-%m-%Y')
    filename=os.path.join(ADDONS_PATH,'report_file_name'+'_' + date + '.xls')
    wb.save(filename)
    file_view=open(filename,'rb')
    file_data=file_view.read()
    out=base64.encodestring(file_data)
    attach_value={
        'field_char':'File_name' + '_' + date + '.xls',
        'field_xml':out,
        }

    act_id=self.env['model.name'].create(attach_value)
    file_view.close()
    return{
        'type':'ir.actions.act_window',
        'view_type':'form',
        'view_mode':'form',
        'res_model':'model.name',
        'res_id':act_id.id,
        'target':'new',
        }

class ClassName(models.TransientModel):
    _name="model.name"


    field_xml=fields.Binary("Download Excel Report")
    field_char=fields.Char("Excel File")

In xml:

在xml:

    <record id="report_id" model="ir.ui.view">
            <field name="name">Report Excel</field>
            <field name="model">model.name</field>
            <field name="arch" type="xml">
                <form string="Excel Report">
                    <group>
                        <field name="field_xml" readonly="1" filename="lead_char" />
                        <field name="field_char" invisible="1" />
                    </group>
                    <footer>
                        <button name="cancel" string="Close" special="cancel"
                            class="oe_link" />
                    </footer>
                </form>
            </field>
        </record>

<record id="action_report" model="ir.actions.act_window">
            <field name="name">Excel Report</field>
            <field name="type">ir.actions.act_window</field>
            <field name="res_model">model.name</field>
            <field name="view_mode">form</field>
            <field name="view_id" ref="report_id" />
            <field name="target">new</field>
        </record>

#2


5  

You can export the current tree view as an excel file with this module: web_export_view

可以使用这个模块将当前树视图导出为excel文件:web_export_view

Or you can build your own excel report with this other modules: report_xls report_xlsx

或者您可以使用其他模块构建自己的excel报表:report_xls report_xlsx

There are modules that already use the report_xls module, but you can look for them in the Odoo Apps or in the OCA repository

有些模块已经使用了report_xls模块,但是您可以在Odoo应用程序或OCA存储库中查找它们

#1


3  

In python:

在python中:

@api.multi
def method_name(self):
    dict={}
    list=[]

    dict['key1']=self.field_name
    dict['key2']=self.field_name
    dict['key3']=self.field_name

    for line in self.field_name:
        dict_1={}
        dict_1['key_sub1']=line.field_name,
        dict_1['key_sub2']=line.field_name,
        dict_1['key_sub3']=line.field_name,
        dict_1['key_sub4']=line.field_name,
        list.append(list)

    dict['key8']=dict_1  

    style0 = xlwt.easyxf('font: name Times New Roman, color-index blue, bold on')
    style1 = xlwt.easyxf('font: name Times New Roman, color-index red, bold on')
    style2 = xlwt.easyxf('font: name Times New Roman, color-index green, bold on')
    style3 = xlwt.easyxf('font: name Times New Roman, color-index black')
    style4 = xlwt.easyxf('font: name Times New Roman, color-index black, bold on')

    wb = xlwt.Workbook()
    ws = wb.add_sheet('A Excel  report')

    ws.write(0, 2, 'Header Name1',  style0)
    ws.write(0, 3, dict['key1'] , style3)
    ws.write(1, 2, 'Header Name2',  style1)
    ws.write(1, 3, dict['key2'] , style3)
    ws.write(2, 2, 'Header Name3',  style2)
    ws.write(2, 3, dict['key3'] , style3)

    ws.write(3, 1, 'Header Name4',  style4)
    ws.write(3, 2, 'Header Name5',  style4)
    ws.write(3, 3, 'Header Name6',  style4)
    ws.write(3, 4, 'Header Name7',  style4)
    n=4
    for vals in dict['key4']:
        ws.write(n, 1, vals['key_sub1'][0] , style3)
        ws.write(n, 2, vals['key_sub2'][0] , style3)
        ws.write(n, 3, vals['key_sub3'][0] , style3)
        ws.write(n, 4, vals['key_sub4'][0] , style3)
        n+=1

    current_date=datetime.now()
    date =current_date.strftime('%d-%m-%Y')
    filename=os.path.join(ADDONS_PATH,'report_file_name'+'_' + date + '.xls')
    wb.save(filename)
    file_view=open(filename,'rb')
    file_data=file_view.read()
    out=base64.encodestring(file_data)
    attach_value={
        'field_char':'File_name' + '_' + date + '.xls',
        'field_xml':out,
        }

    act_id=self.env['model.name'].create(attach_value)
    file_view.close()
    return{
        'type':'ir.actions.act_window',
        'view_type':'form',
        'view_mode':'form',
        'res_model':'model.name',
        'res_id':act_id.id,
        'target':'new',
        }

class ClassName(models.TransientModel):
    _name="model.name"


    field_xml=fields.Binary("Download Excel Report")
    field_char=fields.Char("Excel File")

In xml:

在xml:

    <record id="report_id" model="ir.ui.view">
            <field name="name">Report Excel</field>
            <field name="model">model.name</field>
            <field name="arch" type="xml">
                <form string="Excel Report">
                    <group>
                        <field name="field_xml" readonly="1" filename="lead_char" />
                        <field name="field_char" invisible="1" />
                    </group>
                    <footer>
                        <button name="cancel" string="Close" special="cancel"
                            class="oe_link" />
                    </footer>
                </form>
            </field>
        </record>

<record id="action_report" model="ir.actions.act_window">
            <field name="name">Excel Report</field>
            <field name="type">ir.actions.act_window</field>
            <field name="res_model">model.name</field>
            <field name="view_mode">form</field>
            <field name="view_id" ref="report_id" />
            <field name="target">new</field>
        </record>

#2


5  

You can export the current tree view as an excel file with this module: web_export_view

可以使用这个模块将当前树视图导出为excel文件:web_export_view

Or you can build your own excel report with this other modules: report_xls report_xlsx

或者您可以使用其他模块构建自己的excel报表:report_xls report_xlsx

There are modules that already use the report_xls module, but you can look for them in the Odoo Apps or in the OCA repository

有些模块已经使用了report_xls模块,但是您可以在Odoo应用程序或OCA存储库中查找它们