Forum Discussion

zlatoroh's avatar
14 years ago

Read excel data and copy them to object

Hello,

I have begun with testing our application (written in .NET C#). I have excel with some data about patients(name, surname, sex,...) - every row is a new patients.



I used  your code fore reading form excel:

******************************************************************************************

procedure TestDriver;

var


  Driver : OleVariant;

begin


  // Creates the driver


  // If you connect to an Excel 2007 sheet, use the following method call:


  // Driver := DDT.ExcelDriver('C:\MyFile.xlsx', 'Sheet1', true);


  Driver := DDT.ExcelDriver('C:\MyFile.xls', 'Sheet1');

  

  // Iterates through records


  RecNo := 0;

  while not Driver.EOF do


  begin


    ProcessData; // Processes data


    Driver.Next; // Goes to the next record


  end;

  

  // Closes the driver


  DDT.CloseDriver(Driver.Name);

end;

******************************************************************************************



this works fine but I wold like to save all the data from excel to an object or something like that so it can be used more than once.

Yes I could read one row and run test and then the second row ... But this can't be used in our test.



I have tried something  like this:



******************************************************************************************

function LoadExcel(path : string, list: string) : OleVariant;

var

  test;

  arrayList :  Array[1..10] of OleVariant;//ObjectMapping;

  j,changeArraySize,h: integer;

  driver,TestClass : OleVariant;

  testS,testIme : string;

begin

  TestClass := ODT.Classes.New('ClsExcelTest')   ;

  j := 1;

 driver := DDT.ExcelDriver('D:\podatki.xlsx', 'List1', true);    

  while not driver.EOF do

  begin

     TestClass.AddProperty(DDT.CurrentDriver.ColumnName[0],DDT.CurrentDriver.Value[0]);

     TestClass.AddProperty(DDT.CurrentDriver.ColumnName[1],DDT.CurrentDriver.Value[1]);

     TestClass.AddProperty(DDT.CurrentDriver.ColumnName[2],DDT.CurrentDriver.Value[2]);

     TestClass.AddProperty(DDT.CurrentDriver.ColumnName[3],DDT.CurrentDriver.Value[3]);

     TestClass.AddProperty(DDT.CurrentDriver.ColumnName[4],DDT.CurrentDriver.Value[4]);

     TestClass.AddProperty(DDT.CurrentDriver.ColumnName[5],DDT.CurrentDriver.Value[5]);   

     TestClass.AddProperty(DDT.CurrentDriver.ColumnName[6],DDT.CurrentDriver.Value[6]);   

     TestClass.AddProperty(DDT.CurrentDriver.ColumnName[7],DDT.CurrentDriver.Value[7]);   

     

     arrayList :=TestClass;  

    

    TestClass := ODT.Classes.New('ClsExcelTest')   ;   

    driver.Next; // Goes to the next record

    j := j + 1;

  end;               

            

  DDT.CloseDriver(driver.Name);

  Result := arrayList;

end;

******************************************************************************************



But this doesn't work. This function should return an object with all the data. So I could access data like this:

obj.getPatient(0).name()

obj.getPatient(0).surname()

obj.getPatient(0).sex()

....



This is is easy to do in delphi, java,... but in TC I have big problems.



Can you please help to solve this problem.



Thank you and good day!
  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor
    Have you considered working directly with the spreadsheet via a COM interface?  The DDT driver is best for iterating and then done... But if you want to be able to access, at will, various rows, columns, etc, you're best is a direct interface to Excel.



    There are two articles posted online that describe how to read and write data to excel.  Check out http://smartbear.com/support/viewarticle/24734/ and http://smartbear.com/support/viewarticle/20878/.  While they are not exactly what you are looking for, they show how to create an interface to the Excel spreadsheet and how to access specific cells and the data there in.