Home > front end >  Clear Excel file after one simulation hour anylogic
Clear Excel file after one simulation hour anylogic

Time:12-02

I am transferring some data from Anylogic to Excel. This happens every time an agent enters the wait block, and I store its ID,processing time and due date. Which would look like:

ID p d
1 5 7
2 3 8
3 6 15

After one hour, I would like the Excel file to be cleared (except for the headers 'ID' 'p' and 'd'. (after one hour, the orders are released from the wait block).

So that the next hour, the Excel file will only contain data from orders coming in the second hour

ID p d
4 7 7
5 10 24
6 4 13

I tried doing this using an event, triggered after one hour, with the following code I found in the answer of a similar question:

for (int i=0;i<15;i  ){      //for each column  
    for (int j=2;j<50000;j  ){   //for each row 
            ALtoGA.clearCell(1,j,i);   
    }             
}

However, this only clears the entries in the second row, and I want all my entries to be cleared. Does anyone know how to do this?

CodePudding user response:

I won't answer this question, instead i'll tell you that writing and deleting stuff from an excel file is bad practice...

Instead what you should do is create a class which will have a parameter for each column

public class ExportInfo(){
    public int id;
    public int P;
    public int d;

    public ExportInfo(int id,int P, int d) { 
           this.id=id;
           this.P=P;
           this.d=d;
    }

}

Then what you want is have a collection such a linkedList with elements of type ExportInfo

Then you just add rows to this linkedList

yourList.add(new ExportInfo(a,b,c));

then when you want to remove everything

yourList.clear();

Then you can just export to Excel when it actually provides value to you:

int row=2;
for(ExportInfo e : yourList){
      col=1;
      ALtoGA.setCellNumericValue(e.id,1,row,col  );
      ALtoGA.setCellNumericValue(e.P,1,row,col  );
      ALtoGA.setCellNumericValue(e.d,1,row,col  );
      row  ;
}
ALtoGA.writeFile();

something like this

CodePudding user response:

To delete all data from a table, you can call 'deleteFrom(tableName).execute();'

But you should not write or delete from tables during runtime, slows your model unnecessarily

  • Related