You are here: Home OpenERP Developing Wizard to export something to a csv (Excel) file
Search
Advanced Search…
E-Mail

Webmail: webmail.wyden.com

E-Mail Preferences: postfix.wyden.com/users

E-Mail Administration: postfix.wyden.com

Statistics
Total: 473
Total Pages: 286
Total Folders: 87
Total Files: 18
Total Links: 26
Last modification: 19.04.2012 15:21
 

Wizard to export something to a csv (Excel) file

by Wyden Silvan last modified 15.06.2010 20:08

report.py

***************************************************

import base64
from osv import fields,osv
from tools.translate import _
import time

class Report(osv.osv_memory):
    """
    Wizard to create custom report
    """
    _name = "report"
    _description = "Create Report"
    _columns = {
                'start_date' : fields.date('Start Date', required=True),
                'end_date' : fields.date('End Date', required=True),
                'data': fields.binary('File', readonly=True),
                'name': fields.char('Filename', 16, readonly=True),
                'state': fields.selection( ( ('choose','choose'),   # choose date
                     ('get','get'),         # get the file
                   ) ),
                }
   
    def create_report(self,cr,uid,ids,context={}):
        this = self.browse(cr, uid, ids)[0]
        output = 'Start;Ende'
        output += '\n' + this.start_date + ';' + this.end_date
        print this.start_date
        out=base64.encodestring(output)
        return self.write(cr, uid, ids, {'state':'get', 'data':out, 'name':'test.csv'}, context=context)
       
    _defaults = {
                 'state': lambda *a: 'choose',
                 'start_date' : lambda *a: time.strftime('%Y-%m-%d'),
                 'end_date' : lambda *a: time.strftime('%Y-%m-%d'),
                }

   
Report()

************************************************

report.xml

************************************************

<?xml version="1.0" encoding="UTF-8"?>

<openerp>
    <data>
   
        <!--  =========== VIEWS =========== -->   
   
        <record model="ir.ui.view" id="view_wizard">
            <field name="name">Custom Report</field>
            <field name="model">report</field>
            <field name="type">form</field>
            <field name="priority">16</field>
            <field name="arch" type="xml">
                <form col="3" string="Custom Report">
                    <group col="2" fill="0" height="2500" states="choose">
                        <separator string="Report" colspan="2"/>
                        <field name="start_date" width="200"/>
                        <field name="end_date" width="200"/>
                        <separator colspan="2"/>
                        <group colspan="2">
                            <button special="cancel" icon="gtk-cancel" string="Cancel"/>
                            <button type="object" name="create_report" icon="gtk-go-forward"
                                    string="Create"/>
                        </group>
                        <field invisible="1" name="state"/>
                    </group>
                    <group col="3" fill="0" states="get">
                        <separator colspan="3" string="Export done"/>
                        <field name="name" invisible="1" width="100"/>
                        <field name="data" nolabel="1" readonly="1" width="100" fieldname="name"/>
                    </group>
                </form>
            </field>
        </record>
       
        <!--  =========== ACTIONS =========== -->   
       
        <record model="ir.actions.act_window" id="action_report">
            <field name="name">Custom Report</field>
            <field name="res_model">report</field>
            <field name="view_type">form</field>
            <field name="view_mode">form</field>
            <field name="target">new</field>
        </record>
       
        <!--  =========== MENU ITEMS =========== -->
        <menuitem id="menu_report" name="Report"
                    action="action_report"
                    parent="account.menu_finance_reporting"
                    sequence="40" />
       
       
    </data>
</openerp>
 

thanks for sharing

Posted by Anonymous User at 13.03.2012 05:18
Hi Wyden,
I just want to say thank you very much to share this information,
I've been searching for this topic since a few months a go...
and this information is very helpful to me.

thank you.

thanks for sharing

Posted by Anonymous User at 13.03.2012 05:20
btw, I reshare this information through my blog http://berbagiopenerp.blogspot.com with Indonesian language.

thank you
~Mawaddah (Indonesia)

Very useful

Posted by Anonymous User at 13.04.2012 13:34
But I have a problem with my xml : it seems that the OpenERP engine throws an error when I put the fieldname attribute in <field name="data" nolabel="1" readonly="1" width="100" fieldname="name"/>.

Without this attribute, the export to a file works fine, but the name of this file is something like 'purchase_csv_xx' where xx is an incremental value.

Any idea of how to give a 'real' filename ?

I use OE6.1

Thx a lot,

Stéphane, Belgium