Hi,
I’m facing problem while removing the charts from the spreadsheet using OPN XML SDK v2.
Actual problem is not while saving the file after removing the chart part. But when I open the saved excel file it is telling me the file is not valid and is asking for recovering the file.
The problem seems to be with the drawing and the legacy drawing elements in the file. When I looked into the difference between the saved file and the file which was corrected by excel, the elements(Drawing, Legacy Drawing) have been removed.
I’m using below code for removing the charts from spreadhseet:
using (SpreadsheetDocument document = SpreadsheetDocument.Open(filePath, true)) { WorkbookPart wbPart = document.WorkbookPart; var sheets = wbPart.GetPartsOfType<WorksheetPart>(); foreach (var sheet in sheets) { sheet.DeletePart(sheet.DrawingsPart); sheet.DeleteParts<VmlDrawingPart>(sheet.VmlDrawingParts); //var drawingsParts = sheet.GetPartsOfType<DrawingsPart>(); //if (drawingsParts != null) //{ // foreach (var dwgPart in drawingsParts) // { // dwgPart.DeleteParts<ChartPart>(dwgPart.ChartParts); // //foreach (var chtPart in dwgPart.ChartParts) // //{ // // if (chtPart.ChartSpace != null) // // { // // chtPart.ChartSpace.RemoveAllChildren<Chart>(); // // //Chart cht = chtPart.ChartSpace.GetFirstChild<Chart>(); // // //cht.Remove(); // // } // //} // } // sheet.DeleteParts<DrawingsPart>(drawingsParts); //} //var vmlDrawingsParts = sheet.GetPartsOfType<VmlDrawingPart>(); //if (vmlDrawingsParts != null) //{ // sheet.DeleteParts<VmlDrawingPart>(vmlDrawingsParts); //} } }
Any help in this regard is highly appreciated.
Thanks,
Vinay TC