We help IT Professionals succeed at work.

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

GoldenJag asked
Medium Priority
Last Modified: 2012-05-05
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?

Watch Question


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)
      Return buffer
   End Function 'loadFile

using System;
using System.IO;

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



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?

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)

yeah sorry


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?
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);
 return buffer;

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

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 :)


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 :)

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?


Ok i will get more info from him.  Thanks for all your help.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.