openerp用wizard導入excel數據

作爲一個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))


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章