How to get xlm macro sheet from excel with apache poi XSSFWorkbook?

92 Views Asked by At

I want to get macro sheet and remove it from xlsm document. Excel contains Macro1 and Sheet1.

workbook.getNumberOfSheets()

This returns 1, just Sheet1. It doesn't recognize Macro sheets.

for part in workbook.getRelationParts():
    print(part.getDocumentPart())

result:
Name: /xl/macrosheets/sheet1.xml - Content Type: application/vnd.ms-excel.macrosheet+xml
Name: /xl/worksheets/sheet1.xml - Content Type: application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml
Name: /xl/theme/theme1.xml - Content Type: application/vnd.openxmlformats-officedocument.theme+xml
Name: /xl/styles.xml - Content Type: application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml
Name: /xl/sharedStrings.xml - Content Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sharedStrings+xml

If i remove macrosheet relation document is corrupted.

opcpackage = workbook.getPackage()
macro = opcpackage.getPartsByName(Pattern.compile("/xl/macrosheets/.*.xml")).get(0)
opcpackage.removePart(macro)

wbpart = workbook.getPackagePart()
wbrelcollection = wbpart.getRelationshipsByType("http://schemas.microsoft.com/office/2006/relationships/xlMacrosheet")

for rel in wbrelcollection:
    wbpart.removeRelationship(rel.getId())

workbook.write(outstream)

Do you know any solutions to find and remove macrosheets?

0

There are 0 best solutions below