Using OpenMCDF to modify xls file results in corrupted output

980 views Asked by At

I'm working on a project where I need to edit the contents of existing office files. I'm using the open source library OpenMCDF (https://sourceforge.net/p/openmcdf) which provides easy access to access the data in compound ole files. I've found it excellent for reading data but am having problems writing to them.

I've written a small code sample to demonstrate the problem as per below (note you will need to change the file path). In this case I take a particular stream (./_VBA_PROJECT/VBA/dir) and re-write the contents of the stream truncating the stream by 50 bytes. To demonstrate the corruption, if you open the output file using 7zip and try to export the dir stream you are informed that the dir stream is broken.

The problem only seems to be a problem when you write a stream shorter than the original. Adding more bytes doesn't seem to cause a problem.

Any help here would be much appreciated.

using OpenMcdf;


namespace OpenMcdfTest
{
    class Program
    {
        static void Main(string[] args)
        {
            const string FILE_PATH = @"c:\users\ross\desktop\temp.xls";

            CompoundFile cf = new CompoundFile(FILE_PATH);

            CFStream dirStream = cf.RootStorage.GetStorage("_VBA_PROJECT_CUR").GetStorage("VBA").GetStream("dir");

            byte[] currentData = dirStream.GetData();

            Array.Resize(ref currentData, currentData.Length - 50);

            dirStream.SetData(currentData);

            cf.Save(FILE_PATH + ".edited");
            cf.Close();
        }
    }
}
0

There are 0 answers