Iam trying to open the attached xlsm file using Aspose.Cells.Workbook Open method. Its failing with the error “Invalid Excel2007Xlsx file format”. I can open the same file in excel without any issue.
The attached file was created from xlsx file using the below code
using (excelSheet = SpreadsheetDocument.Open(stream, true))
{
excelSheet.ChangeDocumentType(DocumentFormat.OpenXml.SpreadsheetDocumentType.MacroEnabledWorkbook);
const string VbaProjectRelationShipType = “
http://schemas.microsoft.com/office/2006/relationships/vbaProject
”; VbaProjectPart vbaProjectPart = null;
using (MemoryStream memoryStream = new MemoryStream(documentWithMacroContent, false))
{
using (SpreadsheetDocument documentWithMacro = SpreadsheetDocument.Open(memoryStream, false))
{
WorkbookPart mainPartOfDocumentWithMacro = documentWithMacro.WorkbookPart;
foreach (IdPartPair partPair in mainPartOfDocumentWithMacro.Parts)
{
if (partPair.OpenXmlPart.RelationshipType == VbaProjectRelationShipType)
{
vbaProjectPart = (VbaProjectPart)partPair.OpenXmlPart;
break;
}
}
WorkbookPart mainPartOfWorkbook = spreadSheetContent.WorkbookPart;
if (mainPartOfWorkbook.GetPartsCountOfType() > 0)
{
mainPartOfWorkbook.DeleteParts(mainPartOfWorkbook.GetPartsOfType());
}
if (vbaProjectPart != null)
{
mainPartOfWorkbook.AddPart(vbaProjectPart);
}
}
}
}