12-26-2010 11:28 PM - edited 12-26-2010 11:29 PM
Hello everyone,
First off, I want to thank everybody on this forum. You may not know it, but you have helped me get far with my application.
Forgive my stupidity, as I am a noob with ActionScript. But what I'm trying to do is read an excel file which I have stored in my src folder. Its called "t1.xls".
I am using a library called as3xls and this is what my code looks like:
var xls:ExcelFile = new ExcelFile();
var ba:ByteArray = new ByteArray();
var f:File = File.applicationDirectory;
f = f.resolvePath("t1.xls");
ba = tempFiles.data;
xls.loadFromByteArray(ba);
The ExcelFile class is a class that comes with the as3xls library.
I'm getting an error when doing this, which is basically telling me that my ByteArray is null. I don't know much, but I suspect that I'm not reading the file correctly. Like I said earlier, the file, "t1.xls" is inside my src folder.
I would like to know if I am reading the file properly, and if I am doing something wrong, how can I make a ByteArray from a file in my src folder.
Thanks in advance ![]()
Solved! Go to Solution.
12-26-2010 11:36 PM
hey magicman,
glad to hear you've found help through the forums! ![]()
as for your code, im a little confused as to what tempFile.data represents in this line:
ba = tempFiles.data;
also try re-writing this
var f:File = File.applicationDirectory;
f = f.resolvePath("tl.xls");
to
var f:File = File.applicationDirectory.resolvePath("tl.xls");
good luck!
12-27-2010 02:29 AM - edited 12-27-2010 02:37 AM
i'm assuming here, since i don't see all of your code, but it appears that:
ba = tempFiles.data; xls.loadFromByteArray(ba);
might suppose to be:
ba = f.data; xls.loadFromByteArray(ba);
if that's the case you could reduce your code by writing:
var ba:ByteArray = new ByteArray();
ba = File.applicationDirectory.resolvePath("t1.xls").da ta;
var xls:ExcelFile = new ExcelFile();
xls.loadFromByteArray(ba);
12-27-2010 08:25 AM
Hello,
You need to load the file first before you can access the ByteArray data of that file.
Here is a simple example:
private function onClick(e:Event):void {
loadStatus = "Loading file";
_xlsFile = File.applicationDirectory.resolvePath('sample.xls' );
_xlsFile.addEventListener(Event.COMPLETE, onFileLoaded);
_xlsFile.load();
}
protected function onFileLoaded(event:Event):void
{
var ef:ExcelFile = new ExcelFile();
ef.loadFromByteArray(_xlsFile.data);
loadStatus = "Loaded sheets: " + ef.sheets.length;
}
You must also be aware that as3xls is using Flex framework classes so you wont be able to use that library in pure AS3/QNX project.
Here is the sample Flex Hero project where you can see the whole example.
You will need Flash Builder Burrito to open the project.
12-27-2010 10:10 AM - edited 12-27-2010 10:23 AM
Once again, this forum has proven itself ![]()
You guys are AWESOME!
JRab and TheDarkln, yeah, it was supposed to f, not tempfiles, I was changing variables because I did not want to type so much, but i guess i forgot to change that.
My problem was that I was doing stuff before it was fully loaded. Adding the EventListener fixed this. Thank you guys once again
This is my final code:
private var f:File;
private var xls:ExcelFile;
private var ba:ByteArray;
private function loadFile():void{
f = new File();
ba = new ByteArray();
f = File.applicationDirectory.resolvePath('t5.xls');
f.addEventListener(Event.COMPLETE, onFileLoaded);
f.load();
}
protected function onFileLoaded(event:Event):void
{
xls = new ExcelFile();
xls.loadFromByteArray(f.data);
var sheet:Sheet = xls.sheets[0];
(A BUNCH OF CODE)
}
THANKS ONCE AGAIN TO EVERYONE!
PS: JRab, you are one of those people who have helped me without knowing it. With the CustomCellRenderer with word wrapping. Thanks!
12-27-2010 11:14 AM
@magicman0: its great to be appreciated but even greater to know i've have helped! should you need anything else dont be afraid to ask! good luck with the rest of your playbook development! ![]()
06-17-2011 02:54 AM
Hey quick follow up, I am trying to do the following:
read cell A1 from Book1.xlsx using actionscript. Can you (or anyone) paste their version of code for setting Excel file into a ByteArray and then read the cell by making use of the as3xls-1.0.swc library. I am a newbie to actionscript but I understand basic concepts of brower,parsers, DOM. And if you can please paste ALL lines (please dont exclude like a library import statement for ex). Thanks in advance!
06-17-2011 02:57 AM
And I dont understand the significance of "loading" the file. Aren't you just opening a file and reading from it...I thought loading had more to do with rendering...
06-17-2011 02:57 AM
thanks again in advance!