Skip to content

OpenERP Spreedsheet(ods,xls) report

January 7, 2013

Sample code:

"""
Wizard xml file:
================

<field name="name"/>
<field name="data"/>

<button icon="gtk-print" name="produce_xls" string="Xls" type="object" colspan="2" default_focus="1" />

wizard python file:
==================

Add fields:
----------

"data":fields.binary("File",readonly=True),
"name":fields.char("Filename",16,readonly=True),

Add function;
------------
# above button action  """
def produce_xls(self, cr, uid, ids, context={}):
""" function to create xls """
import StringIO
import base64
try:
import xlwt
except:
raise osv.except_osv('Warning !','Please download python xlwt module from\nhttp://pypi.python.org/packages/source/x/xlwt/xlwt-0.7.2.tar.gz\nand install it')
wiz=self.browse(cr,uid,ids[0])
where_sql = []
""" getting form data for filteration"""
if where_sql:
where_sql = ' or '+' and '.join(where_sql)
else:
where_sql=''
cr.execute('''

select * from sale_order   '''+ where_sql + '''
''')
data = cr.dictfetchall()
record={}
sno=0
wbk = xlwt.Workbook()
style1 = xlwt.easyxf('font: bold on,height 240,color_index 0X36;' 'align: horiz center;''borders: left thin, right thin, top thin')
s1=0
"""adding a worksheet along with name"""
sheet1 = wbk.add_sheet('Work Orders')
s2=1
sheet1.col(0).width = 2000
sheet1.col(1).width = 6000
sheet1.col(2).width = 4000
sheet1.col(3).width = 4000
sheet1.col(4).width = 4000
sheet1.col(5).width = 4000
sheet1.col(6).width = 4000
""" writing field headings """
sheet1.write(s1,0,"Id.",style1)
sheet1.write(s1,1,"Name",style1)
sheet1.write(s1,2,"Label-3",style1)
sheet1.write(s1,3,"Label-4",style1)
sheet1.write(s1,4,"Label-5",style1)
sheet1.write(s1,5,"Label-6",style1)
sheet1.write(s1,6,"Label-7",style1)
"""writing data according to query and filteration in worksheet"""
serial_no=1
for  ele in data:
sheet1.write(s2,0,serial_no)
sheet1.write(s2,1,ele['id'])
sheet1.write(s2,2,ele['date_order'])
sheet1.write(s2,3,ele['name'])
sheet1.write(s2,4,ele['create_date'])
sheet1.write(s2,5,ele['requested_date'])
sheet1.write(s2,6,ele['id'])
serial_no+=1
s2+=1
tot_buf=0
tot_req=0
tot_req_tot=0
tot_tentative_1=0
tot_tentative_2=0
"""Parsing data as string """
file_data=StringIO.StringIO()
o=wbk.save(file_data)
"""string encode of data in wksheet"""
out=base64.encodestring(file_data.getvalue())
"""returning the output xls as binary"""
return self.write(cr, uid, ids, {'data':out, 'name':'Test'+'.xls'}, context=context)

Screenshot from 2013-01-07 10:54:03

Advertisements
6 Comments leave one →
  1. August 11, 2013 1:58 pm

    Is it possible to do the same as this example but in version 7.
    I dont seem to get this working in openerp v7.

    • Rajkumar.S permalink*
      August 13, 2013 11:53 am

      In version 7 above methods is working in GTK(6.1) version only not working in web interface.
      becasue V7 binary field has some core error.

      • Michael permalink
        August 13, 2013 12:42 pm

        Thanks Rajkumar,

        I figured out and managed to do it.

        kind regards, Michael

        On Tue, Aug 13, 2013 at 8:23 AM, Upcomer Blog

  2. edgard pimentel permalink
    January 30, 2015 9:46 am

    HOW TO GENERATE OUPUT EXCEL FROM WIZARD ?

  3. April 6, 2015 8:44 am

    COOL!!! Thank you!!!

  4. karthick G P permalink
    February 20, 2016 9:42 am

    It’s working in version 6 but not in version 7,
    how to retrieve xls output in version 7 from wizard???

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: