Skip to Content
เมนู
คุณต้องลงทะเบียนเพื่อโต้ตอบกับคอมมูนิตี้
คำถามนี้ถูกตั้งค่าสถานะ
4 ตอบกลับ
3909 มุมมอง

Hi all,

I want to create a custom wizard that imports a csv and then compares this data to the data that is already in the database. I then want to select to keep the data, replace or delete the data. i am currently on Odoo8. 

I currently have:

class ExcelImport(models.TransientModel):
    _name = 'excell.import.wizard'

    excel_file_for_import = fields.Binary(string='File for upload')

    def import_data_form_file(self):
        try:
            inputx = StringIO.StringIO()
            inputx.write(base64.decodestring(self.excel_file_for_import))
            book = open_workbook(file_contents=inputx.getvalue())
        except TypeError as e:
            raise ValidationError(u'ERROR: {}'.format(e))
        sheet = book.sheets()[0]

        for i in range(sheet.nrows):
            if i == 0:
                continue
            if i == 1:
                name = sheet.cell(i, 0).value
                vat = sheet.cell(i, 5).value
อวตาร
ละทิ้ง
คำตอบที่ดีที่สุด

Hi, you can follow following link for this:

https://youtu.be/oMnHpHH54QU

Thanks

อวตาร
ละทิ้ง
คำตอบที่ดีที่สุด

Hi  Giano,

I think this video can help you: https://www.youtube.com/watch?v=UOBxxnYDIsM&list=PLSKcWRTtEl5qzvRaI-VTGavfReiHS_EEb&index=1

อวตาร
ละทิ้ง
คำตอบที่ดีที่สุด

HI,

You can check out the import_file function in this file:  https://github.com/odoomates/odooapps/blob/15.0/om_account_bank_statement_import/models/account_bank_statement_import.py which read data from csv file and creates record in odoo

Thanks

อวตาร
ละทิ้ง
คำตอบที่ดีที่สุด

Hi,

You can try this code on the wizard Python file

from odoo import models, fields
import base64
import io
import csv


class ExcelImport(models.TransientModel):

_name = 'excell.import.wizard'


excel_file_for_import = fields.Binary(string='File for upload')
    
      def import_data(self):

  csv_data = base64.b64decode(self.excel_file_for_import)

  data_file = io.StringIO(csv_data.decode(
"utf-8"))

  csv_reader = csv.reader(data_file, delimiter=
',')
 
keys = ['id', 'name']
  //you can
add any fields here
  file_reader = []
  file_reader.
extend(csv_reader)
 
for i in range(len(file_reader)):
  field =
list(map(str, file_reader[i]))
 
values = dict(zip(keys, field))
 
if values:
     
if i == 0:
         
continue
     
else:
            //
browse value must be an id
            record = self.env[
'model.name'].browse(values['id'])
           
if record:
          record.
write({'name': values['name']
  //you can
add the related fields
          })
           
else:
          self.env[
'model.name'].sudo().create({'name':      values['name']
  //you can
add the related fields})


Hope it helps

อวตาร
ละทิ้ง