Excel导出
-
今天从高人那里学来一招不用写controller的excel导出方式,赶快记录下来,不废话,直接上代码:
#下载清单<br /> def down_lines(self, cr, uid,ids, context=None): <br /> addonpath = "/home/davidli/workspace/fiat/"<br /> title = [u'编号',u'零部件号',u'零部件名称',u'数量',u'单位',u'标准成本',u'总额']<br /> lines_obj = self.pool.get("gfiat.com_parts_recipients_lines")<br /> lines_ids=lines_obj.search(cr, uid, [('com_parts_recipients_apply_id', '=', ids[0])])<br /> lines_result=lines_obj.read(cr, uid, lines_ids,['number','parts_no','parts_name','parts_quantity','unit','standard_cost','total_price'], context)<br /> <br /> writedata = []<br /> for row in lines_result:<br /> rowdata = ['','','','','','','']<br /> rowdata[0] = row['number']<br /> rowdata[1] = row['parts_no']<br /> rowdata[2] = row['parts_name']<br /> rowdata[3] = row['parts_quantity']<br /> rowdata[4] = row['unit']<br /> rowdata[5] = row['standard_cost']<br /> rowdata[6] = row['total_price']<br /> writedata.append(rowdata)<br /> <br /> filepath = "/fiat_com_department/static/"+datetime.datetime.now().strftime("%Y%m%d%H%M%S%f")+".xls"<br /><br /> down_xls_report.write_report1_xls(cr, uid, title, writedata, 0, len(title), addonpath+filepath)<br /> <br /> #创建一个sheet<br /> <br /> return {<br /> 'name':"download",<br /># 'url':"http://www.baidu.com",<br /> 'url':"http://localhost:8069"+filepath,<br /> 'type': 'ir.actions.act_url',<br /> 'target': 'self'<br /> }
``` #报表文件写出到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 ```
说明:上面的路径信息都是可以写到系统参数配置的。