导出数据到excel表格
2019-06-26 本文已影响0人
一杯海风_3163
from cStringIO import StringIO
import base64
from datetime import date, timedelta, datetime
import xlwt
from odoo import fields, models, api
from odoo.exceptions import ValidationError, UserError
import datetime
from odoo import _, api, exceptions, fields, models, tools
def batch_export_order_excel(self,context):
"""导出退货报表"""
domain = self._context.get('active_domain', [])
if len(context['active_ids']) == 80:
return_orders = self.search(domain)
else:
domain=[('id', 'in', context['active_ids'])]
return_orders = self.search(domain)
if not return_orders:
raise UserError('数据错误')
data = []
for order in return_orders:
if order.line_ids:
for index,line in enumerate(order.line_ids):
info = dict()
if index ==0:
info['create_date'] = order.create_date
info['transaction_id'] = order.sell_order_id.transaction_id
info['amount'] = order.sell_order_id.amount
info['back_money'] = order.back_money
info['type_id'] = order.type_id.name
info['currency_code'] = order.sell_order_id.currency_id.name
info['note'] = (order.remark or '') + (order.reason_id.name or '')
info['site_order_id'] = order.sell_order_id.site_order_id
info['warehouse_id'] = order.sell_order_id.warehouse_id.name
info['back_state'] = order.back_state
info['back_warehouse_id'] = order.warehouse_id.name
info['back_tracking_number'] = order.tracking_number
info['back_method'] = order.back_method
info['pay_time'] = order.sell_order_id.pay_time
info['send_time'] = order.sell_order_id.send_time
info['back_time'] = order.back_time
info['tracking_number'] = order.sell_order_id.tracking_number
info['partner_name'] = order.sell_order_id.partner_id.name
info['email'] = order.sell_order_id.address_id.email
info['recev'] = order.sell_order_id.recev
info['country'] = order.sell_order_id.address_id.country
info['sate_name'] = order.sell_order_id.address_id.state_name
info['store_name'] = order.sell_order_id.store_id.name
info['company_name'] = order.sell_order_id.company_id.name
info['name'] = order.name
info['sell_order_name'] = order.sell_order_id.name
info['sku'] = line.goods_id.name
info['number'] = line.number
info['cost'] = line.goods_id.cost
info['weight'] = line.goods_id.weight
info['is_second_sell'] = line.is_second_sell
if line.is_second_sell=='yes':
info['is_second_sell'] = u'能'
elif line.is_second_sell=='no':
info['is_second_sell'] = u'不能'
info['no_sell_reason_id'] = line.no_sell_reason_id.name or ''
data.append(info)
else:
info = dict()
info['create_date'] = order.create_date
info['name'] = order.name
info['sell_order_name'] = order.sell_order_id.name
info['transaction_id'] = order.sell_order_id.transaction_id
info['amount'] = order.sell_order_id.amount
info['back_money'] = order.back_money
info['type_id'] = order.type_id.name
info['currency_code'] = order.sell_order_id.currency_id.name
info['note'] = (order.remark or '') + (order.reason_id.name or '')
info['site_order_id'] = order.sell_order_id.site_order_id
info['warehouse_id'] = order.sell_order_id.warehouse_id.name
info['back_state'] = order.back_state
info['back_warehouse_id'] = order.warehouse_id.name
info['back_tracking_number'] = order.tracking_number
info['back_method'] = order.back_method
info['pay_time'] = order.sell_order_id.pay_time
info['send_time'] = order.sell_order_id.send_time
info['back_time'] = order.back_time
info['tracking_number'] = order.sell_order_id.tracking_number
info['partner_name'] = order.sell_order_id.partner_id.name
info['email'] = order.sell_order_id.address_id.email
info['recev'] = order.sell_order_id.recev
info['country'] = order.sell_order_id.address_id.country
info['sate_name'] = order.sell_order_id.address_id.state_name
info['store_name'] = order.sell_order_id.store_id.name
info['company_name'] = order.sell_order_id.company_id.name
data.append(info)
columns = ['制单时间', 'RMA单据ID', '订单号', '交易ID', '订单总金额', '退款金额', '币种', 'SKU', '数量', '成本', '重量','能否二次销售','影响二次销售的原因','原因大类','(备注)退回原因',
'平台原始单号', '发货仓库', '退回状态','退回仓库', '退回快递单号', '快递方式', '付款时间', '发货时间', '退回时间',
'原始快递单号', '客户姓名', '客户邮箱', '收货地址', '国家', '洲/区','店铺', '公司']
columns_value = [
'create_date','name','sell_order_name','transaction_id','amount','back_money','currency_code','sku','number','cost','weight','is_second_sell','no_sell_reason_id','type_id','note',
'site_order_id','warehouse_id','back_state','back_warehouse_id','back_tracking_number','back_method','pay_time','send_time','back_time',
'tracking_number','partner_name','email','recev','country','sate_name','store_name','company_name'
]
workbook = xlwt.Workbook(encoding='utf-8')
worksheet = workbook.add_sheet('sheet1')
# 表头
hr = 3 # 表头占用的行数
header_style = "pattern:pattern solid,fore_colour yellow; " \
"font:height 200, name SimSun, colour_index black, bold on;" \
"align: horz centre, vert center;"
header_style = xlwt.easyxf(header_style)
worksheet.write_merge(0, 1, 0, len(columns), '退货订单.xlsx', header_style)
for col, col_name in enumerate(columns):
worksheet.write(2, col, col_name)
for row, line in enumerate(data):
i = 0
for value_name in columns_value:
worksheet.write(row + hr, i, line.get(value_name,''))
i += 1
# 保存到临时文件,转储到表中,返回弹窗界面
fp = StringIO()
workbook.save(fp)
export_id = self.env['download.excel'].create(
{'excel_file': base64.encodestring(fp.getvalue()), 'file_name': '退货订单.xlsx'})
fp.close()
return {
'view_mode': 'form',
'res_model': 'download.excel',
'res_id': export_id.id,
'view_type': 'form',
'type': 'ir.actions.act_window',
'context': self._context,
'target': 'new',
}