Document Solutions for Excel, .NET Edition | Document Solutions
File Operations / Import and Export Macros
In This Topic
    Import and Export Macros
    In This Topic

    This section summarizes how DsExcel.NET handles the import and export of Excel files containing macros. Using DsExcel.NET, users can load and save Excel files containing macros (.xlsm files) without any hassles. Please note that DsExcel will not execute these macros.

    Typically, this feature has been introduced in order to allow users to load and save macro-enabled spreadsheets. Macros help automate repetitive tasks and hence, reduce significant amount of time while working with spreadsheets. Now, users can load such spreadsheets in DsExcel directly as Xlsm files, modify them easily and quickly and then save them back.

    During the execution of import and export operations on the Excel files, all the macros will also be preserved concurrently along with the data. While opening and saving the Excel workbooks or Excel macro-enabled workbooks, macros will always be imported and exported respectively. The form controls and ActiveX controls are also supported during the import and export operations. DsExcel.NET also provides various import and export options, which can be accessed from the properties present in XlsmOpenOptions and XlsmSaveOptions classes. For more information about import and export options provided by DsExcel, see Import and Export Excel Options.

    When the OpenFileFormat is Xlsm, macros will be imported. When the SaveFileFormat is Xlsm, macros will be exported.

    Note: While preserving the macros on import or export of Excel files, DsExcel will not execute these macros.

    Refer to the following example code in order to import and export macros in spreadsheet documents.

    C#
    Copy Code
    // Open a .xlsm file with file name
    var workbook = new Workbook();
    workbook.Open("testfile.xlsm");
    
    // Save workbook as Excel macro-enabled workbook file
    var workbook = new Workbook();
    workbook.Save("file.xlsm");
    
    // Save workbook as Excel macro enabled workbook into stream
    var workbook = new Workbook();
    var request = WebRequest.CreateHttp("https://path/to/excel/file/upload");
    request.Method = "POST";
    request.ContentType = "application/x-www-form-urlencoded";
    var workbookContent = new MemoryStream();
    workbook.Save(workbookContent, SaveFileFormat.Xlsm);
    workbookContent.Seek(0, SeekOrigin.Begin);
    request.ContentLength = workbookContent.Length;
    using (var reqStream = request.GetRequestStream())
    {
        workbookContent.CopyTo(reqStream);
    }