ComponentOne Excel for .NET
In This Topic
    Loading and Saving Macro enabled Excel Files
    In This Topic

    Excel for .NET supports loading and saving OpenXMLMacro file format. The macro-enabled Excel files (XLSM) written in Visual Basic Application (VBA) language can now be loaded and saved in C1Excel. XLSM is Excel Macro-enabled spreadsheet devised by Microsoft Excel. It contains embedded macros programmed in VBA code. XLSM files are similar to XLSX files, but with macros enabled.

    The following code provides an example of how to load template xls file with VBA macro and save the files in XLSM format:

    1. Double-click Button control in the Toolbox to add it to your form. Change the Name and Text properties of the button, for instance, Name of the button can be btnLoad and Text displayed on it can be Load.
    2. Double-click C1XLBook component in the Toolbox to add it to your form.
    3. Double click Button control on the form to add the btnLoad_Click event and switch to code view.
    4. Add the following code to the Form to add C1.C1Excel namespace:

      To write code in Visual Basic

      Visual Basic
      Copy Code
      Imports C1.C1Excel
      

      To write code in C#

      C#
      Copy Code
      using C1.C1Excel;
      
    5. Add the following code to the btnLoad_Click event to save the workbook in .xlsm format:

      To write code in Visual Basic

      Visual Basic
      Copy Code
      ' copy the book
      Dim xlsFileName As String = "..\..\XlsxTestBin.xlsm"
      C1XLBook1.Load(xlsFileName)
      C1XLBook1.Sheets(0)(0, 0).Value = "It is copy!"
      xlsFileName = xlsFileName.Replace(".xlsm", "Modified.xlsm")
      C1XLBook1.Save(xlsFileName)
      
      ' run Excel file
      System.Diagnostics.Process.Start(xlsFileName)
      

      To write code in C#

      C#
      Copy Code
      // copy the book
      string xlsFileName = "..\\..\\XlsxTestBin.xlsm";
      c1XLBook1.Load(xlsFileName);
      c1XLBook1.Sheets[0][0, 0].Value = "It is copy!";
      xlsFileName = xlsFileName.Replace(".xlsm", "Modified.xlsm");
      c1XLBook1.Save(xlsFileName);
      
      // run Excel file
      System.Diagnostics.Process.Start(xlsFileName);
      
    6. Press F5 to run the project and view the .xslm file, similar to the one given below: