Click here to Skip to main content
15,890,882 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi,

I have uploaded an Excel into the DB using C#.
Now I want to retrieve the same excel file and view as excel. To read the file from DB I have written code which saves as byte file in the path specified. But I'm unable to open the file as an excel.

Please find the code below which I have written to download the file as byte.
Kindly let me know how could I open it as an excel and view.

C#
public void readExcelFromDB()
        {
            SqlConnection conn = new SqlConnection(@"My Connection String");
        

            conn.Open();
            SqlCommand Cmd = new SqlCommand("select ExcelSheet from Test_Excel where ExcelID = 1", conn);
            Cmd.CommandType = CommandType.Text;
            //SqlCommand Cmd = new SqlCommand("select Attachment from Requests where RequestsID = 27", conn);
            

            SqlDataReader Reader = Cmd.ExecuteReader(CommandBehavior.CloseConnection);
            //
            string DocumentName = null;
            FileStream FStream = null;
            BinaryWriter BWriter = null;
            //
            //
            //
            byte[] Binary = null;
            const int ChunkSize = 100;
            int SizeToWrite = 0;
            MemoryStream MStream = null;
            //
            while (Reader.Read())
            {
                DocumentName = Reader["ExcelSheet"].ToString();
                // Create a file to hold the output.
                FStream = new FileStream(@"D:\Mathi_Working\" + DocumentName, FileMode.OpenOrCreate, FileAccess.Write);
                BWriter = new BinaryWriter(FStream);
                Binary = (Reader["ExcelSheet"]) as byte[];
                SizeToWrite = ChunkSize;
                MStream = new MemoryStream(Binary);
                //
                for (int i = 0; i < Binary.GetUpperBound(0) - 1; i = i + ChunkSize)
                {
                    if (i + ChunkSize >= Binary.Length) SizeToWrite = Binary.Length - i;
                    byte[] Chunk = new byte[SizeToWrite];
                    MStream.Read(Chunk, 0, SizeToWrite);
                    BWriter.Write(Chunk);
                    BWriter.Flush();
                }
                BWriter.Close();
                FStream.Close();

            }
            FStream.Dispose();

            conn.Close();

            Process.Start(@"D:\Mathi_Working\" + DocumentName);
        }

Thanks & Regards,
Mathi.
Posted
Comments
Kenneth Haugland 19-Jul-13 9:07am    
Normally it is OleDb that is used. Are you sure SQL supports excel files?
Maciej Los 19-Jul-13 9:39am    
My virtual 5!
Mathi2code 22-Jul-13 4:52am    
Kenneth,
While saving it is saving as bytes the Datatype I have used to save excel file is Varbinary(MAX). So I think it will work...
Mathi2code 22-Jul-13 4:53am    
Maciej,

What do you mean by My virtual 5!

1 solution

 
Share this answer
 
v2
Comments
Mathi2code 23-Jul-13 2:22am    
Thanks Sampath, the first link itself worked fine.
Sampath Lokuge 23-Jul-13 3:12am    
@Mathi2code Glad to hear that it helped.Cheers.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900