从 SQL DB 检索时 Excel 文件损坏

发布于 2024-12-12 04:42:21 字数 1954 浏览 0 评论 0原文

我正在使用将 Excel 文件(以及其他文件类型,例如 PDF)存储为二进制数据的 SQL DB。我使用以下代码将这些文件提取到文件系统上。

问题: PDF 文件输出得很好。但对于 Excel,文件已创建,当我尝试打开它们时,它们崩溃或只是给我垃圾文本。

我正在使用前一个编写此应用程序的人提供的代码来检索文件。这段代码使用了我不完全理解的 OpenMcdf,因为我找不到有用的在线文档。

//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);
}

你知道这里发生了什么吗?为什么excel被破坏了?尽管搜索了几个小时,我在网上还是找不到很多东西!

任何想法、建议或解决方案将不胜感激。

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吻风 2024-12-19 04:42:21

按如下方式更改 SaveToDisk 逻辑:

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

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;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文