Load Excel into byte Array.......

HI, i am new to C# and i need to know if anyone can help me.

I need to know how to:

1. Take an excel file and load it into a byte array and then
2. Load that byte array into a stream object.

can anyone help me with this?

Thanks!
GoldenJagAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mikal613Commented:
Function loadFileintoBA(filename As String) As Byte()
      Dim fs As New FileStream(filename, FileMode.Open)
      Dim buffer(CInt(fs.Length)) As Byte
      fs.Read(buffer, 0, buffer.Length)
      fs.Close()
     
      Return buffer
   End Function 'loadFile

or
using System;
using System.IO;

 public FileByteArray(string fileName)
    {
        stream = new FileStream(fileName, FileMode.Open);
    }

0
GoldenJagAuthor Commented:
Mikal613

the first solution looks like it was done in VB and the second solution in C#.  Is this correct?

If so, in your second solution where/how is the excel file being loaded into a byte array?
0
c_myersCommented:
Well, the System.IO.FileStream class will load the file into a stream. From there you can get it into a byte array (using the .Read() method on the stream).

(there are numerous examples of using FileStream in the .NET Framework SDK)

Also, for your convenience, you might check out the BinaryReader class which helps with reading binary files (or BinaryWriter for writing binary)
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Mikal613Commented:
yeah sorry
0
GoldenJagAuthor Commented:
ok c_myers ,

In my original question i asked if i can load the excel file into a byte array first and then load it into a stream object.

Can this be done or do i need to load it as a stream object first and then a byte array?
0
Mikal613Commented:
byte[] loadFileintoBA(string filename)
{
 FileStream fs = new FileStream(filename, FileMode.Open);
 byte[] buffer = new byte[System.Convert.ToInt32(fs.Length)];
 fs.Read(buffer, 0, buffer.Length);
 fs.Close();
 return buffer;
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
c_myersCommented:
Author:

The only way to read and write files in .NET is through streams, so there's no way to get it into a byte array without having first used a stream.

Your questions are curious though, and suggest that you might be approaching your problem from the wrong direction. What is it, exactly, you're trying to do? Maybe I can help you solve your problem instead of solving your solution :)
0
GoldenJagAuthor Commented:
Thanks c_myers.

The thing is that my boss knows that i am learning c# and he assigns me task like this one to aid in the application we are developing.  All he told me was to load an excel file into a byte array and then take that byte array and load it into a stream object. (period) .... now since i am not that knowledgeable i ask these questions on EE to help guide me in the right direction.  So, if you (EE) say that i can only load the excel file as a string first and then a byte array and my boss is telling something different then i get puzzled.  So that is why i ask all the questions :)
0
c_myersCommented:
Ok, well, what your boss is asking for doesn't quite make sense.

Perhaps you can get him/her to elaborate on what the requirements are?

Perhaps your boss just wants you to open the file as a stream and provide that to another application?
0
GoldenJagAuthor Commented:
Ok i will get more info from him.  Thanks for all your help.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.