Skip to main content
Known Participant
June 23, 2013
Question

Writing Files to Excel in AS3

  • June 23, 2013
  • 1 reply
  • 2676 views

Hi,

i have a game that automatically records players interactions. Now I want to write the data to an excel file and use the formular functions of excel.

I was able to write to cvs file but I am aware I cant use the excel formular with this approach.

what do I need to do?

here is the code:

//in spacegame class

public var file:File = File.desktopDirectory.resolvePath( "LogOutput_2Player_1Cannons_DANCEMAT.xlsx" );

public var stream:FileStream = new FileStream();

 

                    public function logString(s:String): void

                    {

                              var today = new Date();

                              stream.open(file,FileMode.APPEND);

                              stream.writeUTFBytes(today.getDate()+"/"+(today.getMonth()+1)+"/"+today.getFullYear()+ "-" +today.getHours()+":"+today.getMinutes()+ ":"+today.getSeconds());

                              stream.writeUTFBytes(":"+s+"\r\n");

                              stream.close();

}

//in player class

private function onKeyPress(evt:KeyboardEvent)

                    {

                              if (evt.keyCode == 39 )

                              {

                                        rightArrow = true;

                                        spacegame.instance.rightCircle.visible = true;

                                        spacegame.instance.logString("Player 1 PRESSES RIGHT KEY");

}

}

when you run the game and player interact , an excel file is created with this data in it:

23/6/2013-21:19:55:324:Player 1 PRESSES RIGHT KEY

here it is in one column but I want the file to put the date in one column, the time in one column, player1 in one column and presses right ke in one column

Regards

chiggy

This topic has been closed for replies.

1 reply

Inspiring
June 24, 2013

What do you use for a Library to create the Excel file?

June 27, 2013

It's an AIR app - so he's using the File class to just write out the file. Google 'AS3 CSV' and you'll find examples and pre-built classes. Though I do see he's writing xlsx which I'm not sure if it's plain text or not liks a CSV file is. I've only written CSV files with this method, and those are fine.

As for the OP's question: You want to write an Array with writeUTFBytes - not a string. If you write an array you'll get comma separated values. So, stick all your data - date, time, player1, etc. into an array then write that out all at once. I do like:

stream.writeUTFBytes( "\n" + newLine ); //where newLine is an array