Hinzufügen von VBA-Modul und Code mit Aspose.Cells
Contents
[
Hide
]
Aspose.Cells ermöglicht es Ihnen, ein neues VBA-Modul und Makrocode mit Aspose.Cells hinzuzufügen. Bitte verwenden Sie dieWorkbook.getVbaProject().getModules().add()-Methode, um das neue VBA-Modul in der Arbeitsmappe hinzuzufügen
Hinzufügen von VBA-Modul und Code mit Aspose.Cells
Der folgende Beispielcode erstellt eine neue Arbeitsmappe, fügt ein neues VBA-Modul und einen neuen Makrocode hinzu und speichert die Ausgabe im Format XLSM. Einmal öffnen Sie die Ausgabedatei XLSM in Microsoft Excel und klicken auf dieEntwickler > Visual Basic Menübefehle sehen Sie ein Modul namens “TestModule” und darin sehen Sie den folgenden Makrocode.
Sub ShowMessage()
MsgBox "Welcome to Aspose!"
End Sub
Beispielcode
Hier ist ein Beispielcode zum Generieren der Ausgabedatei XLSM mit VBA-Modul und Makrocode.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getDataDir(AddVBAModuleAndCode.class); | |
// Create new workbook | |
Workbook workbook = new Workbook(); | |
// Access first worksheet | |
Worksheet worksheet = workbook.getWorksheets().get(0); | |
// Add VBA Module | |
int idx = workbook.getVbaProject().getModules().add(worksheet); | |
// Access the VBA Module, set its name and codes | |
VbaModule module = workbook.getVbaProject().getModules().get(idx); | |
module.setName("TestModule"); | |
module.setCodes("Sub ShowMessage()" + "\r\n" + " MsgBox \"Welcome to Aspose!\"" + "\r\n" + "End Sub"); | |
// Save the workbook | |
workbook.save(dataDir + "output.xlsm", SaveFormat.XLSM); | |