c# reading varbinary or byte[] to .xlsx

Posted on 2012-08-22
Last Modified: 2012-08-24
I am working with a c# in visual studio 2010 and MS SQL Server 2008 R2.

Another application is inserting .xlsx files into an SQL table as varbinary(max).  I need to pull these from the table and read the contents as if they are .xlsx files.

First, do I read these as byte[] to get the varybinary out of the database?

Then, once I have my file binary/byte string how to I convert this to a file that can be read as an excel workbook in C# with the SpreadsheetDocument class (using DocumentFormat.openxml, openxml.packaging, and openxml.spreadsheet).

Question by:HyperBPP
    LVL 6

    Author Comment

    Figured it out (assumes fileBinary from database):

    MemoryStream excelMemStream = new MemoryStream();

    excelMemStream.Write(fileBinary, 0, fileBinary.Length);

     SpreadsheetDocument mySpreadsheet = SpreadsheetDocument.Open(excelMemStream, false);

    Although, this seems slow.  Anyone know faster solution?
    LVL 96

    Accepted Solution

    1) How slow?

    2) You should be able to combine 2 statements, but I don't think that it would help much:

        MemoryStream excelMemStream = new MemoryStream(fileBinary);

    Open in new window


    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
    Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
    This video discusses moving either the default database or any database to a new volume.

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now