Chen Jian

导航

 

来自:http://blog.csdn.net/yumingbuzhongyao/article/details/18669183

作为一个quick note吧。
OE里的csv导入数据功能形同摆设,通俗地说就是弱爆了。
今天尝试一下用excel文件来导入数据。
在python里读取excel格式的lib很多,这里我选用的是xlrd。

上代码先:

 

# -*- coding: utf-8 -*-

from osv import osv, fields
import time, xlrd, base64


class bank_bill_import(osv.osv_memory):
    
    _name = "fg_account.bank_bill.import.wizard"
    _description = "导入账单"
    
    _columns = {
        'excel': fields.binary('excel文件', filters='*.xls'),
    }
    
    def import_bill(self, cr, uid, ids, context=None):
        for wiz in self.browse(cr,uid,ids):
            if not wiz.excel: continue
            
            excel = xlrd.open_workbook(file_contents=base64.decodestring(wiz.excel))
            sh = excel.sheet_by_index(0)
            print sh.name, sh.nrows, sh.ncols
            for rx in range(sh.nrows):
                for ry in range(sh.ncols):
                    print sh.cell(rx, ry).value
                    #这里做爱做的事情
            
        return {'type': 'ir.actions.act_window_close'}

其实重点就在于:

excel =xlrd.open_workbook(file_contents=base64.decodestring(wiz.excel))

posted on 2014-02-10 15:46  Chen Jian  阅读(883)  评论(1编辑  收藏  举报