Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6761375
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:10:56+00:00 2026-05-26T14:10:56+00:00

I am working with a SQL DB that stores Excel files (along with other

  • 0

I am working with a SQL DB that stores Excel files (along with other file types such as PDF) as binary data. I use the following code to extract these files onto the file system.

The Problem:
PDF files come out just fine. But for Excel, the files get created and when I try to open them, they crash or just give me garbage text.

I am using this code from the previous guy who wrote this app for retrieving files. This code uses OpenMcdf which I don’t fully understand because I couldn’t find useful online documentation for it.

//execution starts here
public override void SaveToDisk()
{

    byte[] keys = { (byte)0xd0, (byte)0xcf };


    //Searches through m_RawOleObject for combination of 'keys'
    int offset = Utils.SearchBytes(m_RawOleObject, keys); //returns '60' in case of Excel and '66' in case of Pdf

    //m_RawOleOjbect contains the data from the sqlDataReader (the binary data from the column.)
    m_RawOleObject = strip(m_RawOleObject, offset);

    MemoryStream ms = new MemoryStream(m_RawOleObject);
    CompoundFile cf = new CompoundFile(ms);
    GetStorageByName(cf.RootStorage, m_StorageName);

    if (Storage != null)
    {
        if (Storage is CFStream)
        {
            m_RawOleObject = (Storage as CFStream).GetData();
        }
        m_filename = System.IO.Path.Combine(STOREPATH, Utils.CombineFilenameWithExtension(Filename, m_extension));

        WriteToFile(m_filename, m_RawOleObject);
    }

}

protected void WriteToFile(string fn, byte[] obj)
{
    fn = GetNextAvailableFilename(fn, 0);
    FileStream fs = new FileStream(fn, FileMode.Create);
    BinaryWriter writer = new BinaryWriter(fs);
    writer.Write(obj);
    writer.Close();
    fs.Close();
    fs.Dispose();
}


protected void GetStorageByName(CFStorage cfs, string name)
{
    VisitedEntryAction va = delegate(CFItem target)
    {
        if (target is CFStorage)
        {
            GetStorageByName((CFStorage)target, name);
        }
        else
        {
            if (target.Name == name)
                Storage = target;
        }
    };

    //Visit NON-recursively (first level only)
    cfs.VisitEntries(va, false);
}

Any ideas what’s happening here? why are the excel corrupted? I couldn’t find a lot online despite hours of search!

any ideas, suggestions, or solutions will be appreciated.

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T14:10:56+00:00Added an answer on May 26, 2026 at 2:10 pm

    Change your SaveToDisk logic as follows:

    public override void SaveToDisk()
    {
        byte[] keys = { (byte)0xd0, (byte)0xcf, (byte)0x11, (byte)0xe0, (byte)0xa1, (byte)0xb1, (byte)0x1a, (byte)0xe1 };
        int offset = Utils.SearchBytes(m_RawOleObject, keys);
        using (MemoryStream ms = new MemoryStream(strip(m_RawOleObject, offset)))
        {
            CompoundFile cf = new CompoundFile(ms, UpdateMode.ReadOnly, true, true);
            m_filename = GetNextAvailableFilename(System.IO.Path.Combine(STOREPATH, Utils.CombineFilenameWithExtension(Filename, m_extension)), 0);
            using (var fs = new FileStream(m_filename, FileMode.Create))
            {
                cf.Save(fs);
                cf.Close();
            }
        }
    
        //Workbook would be saved as hidden in previous step
        Microsoft.Office.Interop.Excel.Application xlApp = null;
        Microsoft.Office.Interop.Excel.Workbook xlWb = null;
        try
        {
            xlApp = new Microsoft.Office.Interop.Excel.Application();
            xlWb = xlApp.Workbooks.Open(m_filename);
            xlWb.CheckCompatibility = false;
    
            foreach (Window wn in xlApp.Windows)
            {
                wn.Visible = true;
            }
            xlWb.Save();
            xlWb.Close();
        }
        catch (Exception e)
        {
            //TODO: Log error and continue
        }
        finally
        {
            if (xlWb != null)
                Marshal.ReleaseComObject(xlWb);
            if (xlApp != null)
                Marshal.ReleaseComObject(xlApp);
            xlApp = null;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working with a legacy system that stores its data in individual files
I am working on an Access adp that has its data stored in sql-server.
I'm working on an email solution in SQL Server ONLY that will use Database
we have WinForms app which stores data in SQL Server (2000, we are working
I am working in SQL Server 2005. I have a table that lists stores
I have the session created and it stores the data, that is working 100%;
I'm working with some imported data that stores details about whether a room is
I'm working with a program that runs lengthy SQL queries and stores the processed
I'm working on a stored proc that executes some dynamic sql. Here's the example
I am working on a SQL query that reads from a SQLServer database to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.