I developed a custom import option for update the one2many table, for example I have a binary field for selecting xls or csv, then I have one button for import, this function will need to reterive the csv or xls data to create one2many records. In this code .csv is working fine but can't import .xls file. How to import .xls file.
def records_import(self, cr, uid, ids, context=None):
supplier_obj = self.pool.get('res.partner')
for price in self.browse(cr, uid, ids, context=context):
fileName, fileExtension = os.path.splitext(price.file_to_import_fname)
if fileExtension != '.csv':
raise osv.except_osv(_("Warning !"), _("Change the file type as CSV"))
price_file = unicode(base64.decodestring(price.import_payment), 'windows-1252', 'strict').split('\n')
line_count = 0
ss = len(price_file)
for line in price_file:
line_count += 1
if line_count > 1:
if line_count < ss -1:
line_contents = line.split(',')
if not line_contents:
break
Name = line_contents[0].strip()
Amount = line_contents[1].strip()
supplier_id = supplier_obj.search(cr, uid, [('name', '=', Name)])
if supplier_id == []:
supplier = {
'name':Name
}
new_supplier = supplier_obj.create(cr, uid, supplier)
else:
for supp in supplier_obj.browse(cr, uid, supplier_id):
new_supplier = supp.id
price_line_exist = self.pool.get('sample.lines').search(cr, uid, [('name', '=', new_supplier),('payment_id', '=', price.id),('amount', '=', Amount)])
if price_line_exist == []:
price_upload_dict = {
'name' : new_supplier,
'amount': Amount
}
self.pool.get('sample.lines').create(cr, uid, price_upload_dict)
self.write(cr, uid, price.id,{'upload':True},context=context)
return {}