We help IT Professionals succeed at work.

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

GoldenJag
GoldenJag asked
on
Medium Priority
1,380 Views
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?

Thanks!
Comment
Watch Question

CERTIFIED EXPERT

Commented:
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);
    }

Author

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?

Commented:
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)
CERTIFIED EXPERT

Commented:
yeah sorry

Author

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?
CERTIFIED EXPERT
Commented:
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;
}

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

Ask the Experts
Commented:
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 :)

Author

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

Commented:
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?

Author

Commented:
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.

OR

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.