Navigation

    Odoo 中文社区

    • Register
    • Login
    • Search
    • Categories
    • Tags
    • Popular
    • Users
    • Groups

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

    由于系统升迁的原因,本论坛部分较早期的内容存在格式和链接损坏失效的问题,并非本论坛系统本身的缺陷,望谅解

    本社区没有维护任何QQ群讨论组,任何与本社区同名的QQ群讨论组的言论与本社区无关!

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

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

    Excel导出

    Odoo 开发与实施交流
    3
    4
    2822
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • P
      panyi5202 last edited by

      今天从高人那里学来一招不用写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 ```

      说明:上面的路径信息都是可以写到系统参数配置的。
      1 Reply Last reply Reply Quote 0
      • C
        ccdos last edited by

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

        1 Reply Last reply Reply Quote 0
        • S
          sunk last edited by

          好贴,记录下

          1 Reply Last reply Reply Quote 0
          • First post
            Last post