Odoo中文社区可以通过以下三个域名访问:shine-it.net , odoocn.org,odoo.net.cn

原论坛用户的基本信息和发帖这里都予以保留,请注意:原论坛用户无需重新注册新用户,但是您的密码需要重置

开发人员可以登录gitter讨论组: http://gitter.im/odoo-china/Talk, 需要github账号

如果您登录系统碰到问题,请在微信公众号留言:

Excel导出



  • 今天从高人那里学来一招不用写controller的excel导出方式,赶快记录下来,不废话,直接上代码:

    #下载清单<br />&nbsp; &nbsp; def down_lines(self, cr, uid,ids, context=None): <br />&nbsp; &nbsp; &nbsp; &nbsp; addonpath = &quot;/home/davidli/workspace/fiat/&quot;<br />&nbsp; &nbsp; &nbsp; &nbsp; title = [u&#039;编号&#039;,u&#039;零部件号&#039;,u&#039;零部件名称&#039;,u&#039;数量&#039;,u&#039;单位&#039;,u&#039;标准成本&#039;,u&#039;总额&#039;]<br />&nbsp; &nbsp; &nbsp; &nbsp; lines_obj = self.pool.get(&quot;gfiat.com_parts_recipients_lines&quot;)<br />&nbsp; &nbsp; &nbsp; &nbsp; lines_ids=lines_obj.search(cr, uid, [(&#039;com_parts_recipients_apply_id&#039;, &#039;=&#039;, ids[0])])<br />&nbsp; &nbsp; &nbsp; &nbsp; lines_result=lines_obj.read(cr, uid, lines_ids,&#91;&#039;number&#039;,&#039;parts_no&#039;,&#039;parts_name&#039;,&#039;parts_quantity&#039;,&#039;unit&#039;,&#039;standard_cost&#039;,&#039;total_price&#039;], context)<br />&nbsp; &nbsp; &nbsp; &nbsp; <br />&nbsp; &nbsp; &nbsp; &nbsp; writedata = &#91;]<br />&nbsp; &nbsp; &nbsp; &nbsp; for row in lines_result:<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rowdata = &#91;&#039;&#039;,&#039;&#039;,&#039;&#039;,&#039;&#039;,&#039;&#039;,&#039;&#039;,&#039;&#039;]<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rowdata[0] = row&#91;&#039;number&#039;]<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rowdata[1] = row&#91;&#039;parts_no&#039;]<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rowdata[2] = row&#91;&#039;parts_name&#039;]<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rowdata[3] = row&#91;&#039;parts_quantity&#039;]<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rowdata[4] = row&#91;&#039;unit&#039;]<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rowdata[5] = row&#91;&#039;standard_cost&#039;]<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rowdata[6] = row&#91;&#039;total_price&#039;]<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; writedata.append(rowdata)<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />&nbsp; &nbsp; &nbsp; &nbsp; filepath = &quot;/fiat_com_department/static/&quot;+datetime.datetime.now().strftime(&quot;%Y%m%d%H%M%S%f&quot;)+&quot;.xls&quot;<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; down_xls_report.write_report1_xls(cr, uid, title, writedata, 0, len(title), addonpath+filepath)<br />&nbsp; &nbsp; &nbsp; &nbsp; <br />&nbsp; &nbsp; &nbsp; &nbsp; #创建一个sheet<br />&nbsp; &nbsp; &nbsp;  <br />&nbsp; &nbsp; &nbsp; &nbsp; return {<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &#039;name&#039;:&quot;download&quot;,<br />#&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  &#039;url&#039;:&quot;http://www.baidu.com&quot;,<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &#039;url&#039;:&quot;http://localhost:8069&quot;+filepath,<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &#039;type&#039;: &#039;ir.actions.act_url&#039;,<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &#039;target&#039;: &#039;self&#039;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }
    

    ``` #报表文件写出到excel文件
        def write_report1_xls(self, cr, uid,title,data,startrow,collen,filepath):
            #collen = 11
            wbk = xlwt.Workbook()
            sheet = wbk.add_sheet('sheet 1',cell_overwrite_ok=True)
            x=0
            while x<collen:
                sheet.write(startrow,x,title[x])
                x+=1
               
            row_index = startrow+1
            for row in data:
                col_index = 0
                while col_index<collen:
                    sheet.write(row_index,col_index,row[col_index])
                    col_index += 1
                   
                row_index += 1
               
            #wbk.save('/home/stevezhou/zambon201403/testwrite.xls')
            wbk.save(filepath)
            print "write file %s finished!" % filepath ```

    说明:上面的路径信息都是可以写到系统参数配置的。


  • 很不错,赞一下。
    变成了静态文件,不知道会不会有 权限问题?



  • 好贴,记录下