Process an input Excel file and serialize its rows

1.1k Views Asked by At

I have to solve an exercise, but I can't solve one error. I don't have so much knowledge about Boo language, sorry. My code is:

public class Item (IIDataReaderLoadable):
  Sequence as long
  Code as string
  Description as string
  Weight as decimal
  Id as Guid


  def LoadFromReader(reader as IDataReader):
    Sequence = long.Parse(reader[0].ToString());
    Code = reader[1].ToString();
    Weight = decimal.Parse(reader[2].ToString());
    Description = reader[3].ToString();
    Id = Guid.Parse(reader[4].ToString());
  TableName as string:  
    get:
        return "Hoja1$"


 operation read_MasterData_etlexcel:
   log = ProcessContext.GetLogger()
   file = ProcessContext.InputFile
   log.Info("Reading $file")
   for Data in EntityReader[of Item].Read(file):
       yield Row.FromObject(Data)

 operation print_etlexcel:
   log = ProcessContext.GetLogger()
   for row in rows:
      log.Info(row.ToString())
      yield row

def serialize_row(it as Object, id as Guid):
   serializer = XmlSerializer(typeof(Item))
   writer = FileStream("output" + id.ToString() +".xml", FileMode.Create);
   serializer.Serialize(writer, it);
   writer.Close();

serialize_row(Item, Item.Id)

process process_owners_etlexcel:
   read_MasterData_etlexcel()
   print_etlexcel()

When I execute it in a command window I get the next error:

2018-05-14 14:18:44.0479 [Error] [Mss.Etl.DSLLoader.EtlSetup] Cannot execute ./e
xcelfile/import.boo BCE0000: C:\Program Files\Mecalux\GnaService2015\excelfile\i
mport.boo(57,30): BCE0020: Boo.Lang.Compiler.CompilerError: An instance of type
'Mss.Item' is required to access non static member 'Id'.

I want to read an Excel file that contains some columns and I have to créate a boo script that recover the content of my Excel file, then I have to map each row from the Excel file into an object of you my class Ítem, and serialize the object in a XML file

Thanks

1

There are 1 best solutions below

0
On

The bug is on this line:

serialize_row(Item, Item.Id)

The Item.Id field is a member field not a static field, which means you need an instance. It looks like you're calling it there as as a static so its blowing up. I'm not sure what the solution is because you have a couple of macros there that are not defined in the code example so I'm not sure what they're doing but I would think you either need to remove that line or pass in a member id or a random one.

I would have to guess this is the solution:

item = Item()
serialize_row(item, item.Id)