EPPlusSoftware / EPPlus

EPPlus-Excel spreadsheets for .NET
https://epplussoftware.com
Other
1.83k stars 279 forks source link

Copying a sheet containing an OLEObject corrupts the workbook #360

Open craigbrown opened 3 years ago

craigbrown commented 3 years ago

This is an obscure one. I have a worksheet with an OLE Object on it - an Equation object from a very old version of Excel which wasn't visible on the sheet (and I didn't actually realise it was there). When I copy this sheet (either in the same workbook or a new workbook) and then open it in Excel, I get a message from Excel saying that the file is corrupt.

From closer inspection of the XML, it looks like the OLE data is being copied inside the sheet1.xml file, but some of the necessary files aren't:

xl/drawings/drawing1.xml                ✅ copied
xl/drawings/vmlDrawing1.vml             ✅ copied
xl/drawings/_rels/vmlDrawing1.vml.rels  ❌ not copied
xl/embeddings/oleObject1.bin            ❌ not copied
xl/media/image1.wmf                     ❌ not copied
xl/worksheets/_rels/sheet1.xml.rels     ❌ references missing
[Content_Types].xml                     ❌ references missing

If I manually copy the missing files and add the missing lines to sheet1.xml.rels and [Content_Types].xml then I can open the file in Excel without it complaining about it being corrupt.

I've attached a basic file containing an OLE object to demonstrate the bug, and here's some code which will produce a faulty file:

public void CopyWorksheetWithOLEObject()
{
    const string testFilePath = @"C:\OLEObjectTest.xlsx";
    const string outputFilePath = @"C:\OLEObjectTest_Output.xlsx";
    using (var pck = new ExcelPackage(new FileInfo(testFilePath)))
    {
        var ws = pck.Workbook.Worksheets["Sheet1"];
        using (var newPck = new ExcelPackage(new FileInfo(outputFilePath)))
        {
            newPck.Workbook.Worksheets.Add("Sheet1", ws);
            newPck.Save();
        }
    }
}

Probably a very low impact bug, but still thought it was worth letting you know that this problem exists.

OLEObjectTest.xlsx

JanKallman commented 3 years ago

Ole objects are currently not supported by EPPlus, so there are not logic to copy them either. However, it might be a good idea to make sure the copy worksheet copies the bin file and the relations to preserve the objects.