• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 862
  • Last Modified:

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!
0
GoldenJag
Asked:
GoldenJag
  • 4
  • 3
  • 3
2 Solutions
 
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 4
  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now