Edit XLSM Formats in C#
Native and high performance XLSM document editing using server-side Aspose.Cells for .NET APIs, without the use of any software like Microsoft or Adobe PDF.
How to Edit XLSM File Using C#
In order to edit XLSM file, we’ll use Aspose.Cells for .NET API which is a feature-rich, powerful and easy to use API for C# platform for any editor. Open NuGet package manager, search for Aspose.Cells and install. You may also use the following command from the Package Manager Console.
Command
PM> Install-Package Aspose.Cells
Steps for Editing XLSM Files in C#
A basic document editing with Aspose.Cells for .NET APIs can be done with just few lines of code.
- Include the namespace in your class file
- Load your sample file.
- Add and access the new worksheet of the workbook.
- Get the desired cell(s) of the worksheet and put the value in the cell(s).
- Insert PivotTable and set the style
- Use Save method to save the workbook as XLSM file.
System Requirements
Our APIs are supported on all major platforms and Operating Systems. Before executing the code below, please make sure that you have the following prerequisites on your system.
- Microsoft Windows or a compatible OS with .NET Framework, .NET Core, Windows Azure, Mono or Xamarin Platforms
- Development environment like Microsoft Visual Studio
- Add reference to the Aspose.Cells for .NET DLL in your project - Install from NuGet using the Download button above
Edit XLSM Files - C#
//load your sample file | |
Workbook book = new Workbook("sample.xlsm"); | |
//add new worksheet | |
Worksheet sheet = book.Worksheets.Add("NewSheet"); | |
Cells cells = sheet.Cells; | |
//add some data | |
cells[0, 0].Value = "fruit"; | |
cells[1, 0].Value = "grape"; | |
cells[2, 0].Value = "blueberry"; | |
cells[3, 0].Value = "kiwi"; | |
cells[4, 0].Value = "cherry"; | |
cells[5, 0].Value = "grape"; | |
cells[6, 0].Value = "blueberry"; | |
cells[7, 0].Value = "kiwi"; | |
cells[8, 0].Value = "cherry"; | |
cells[0, 1].Value = "year"; | |
cells[1, 1].Value = 2020; | |
cells[2, 1].Value = 2020; | |
cells[3, 1].Value = 2020; | |
cells[4, 1].Value = 2020; | |
cells[5, 1].Value = 2021; | |
cells[6, 1].Value = 2021; | |
cells[7, 1].Value = 2021; | |
cells[8, 1].Value = 2021; | |
cells[0, 2].Value = "amount"; | |
cells[1, 2].Value = 50; | |
cells[2, 2].Value = 60; | |
cells[3, 2].Value = 70; | |
cells[4, 2].Value = 80; | |
cells[5, 2].Value = 90; | |
cells[6, 2].Value = 100; | |
cells[7, 2].Value = 110; | |
cells[8, 2].Value = 120; | |
PivotTableCollection pivots = sheet.PivotTables; | |
//Add a PivotTable | |
int pivotIndex = pivots.Add("=NewSheet!A1:C9", "A12", "TestPivotTable"); | |
PivotTable pivot = pivots[pivotIndex]; | |
//Add PivotField for Rows area | |
pivot.AddFieldToArea(PivotFieldType.Row, "fruit"); | |
//Add PivotField for Columns area | |
pivot.AddFieldToArea(PivotFieldType.Column, "year"); | |
//Add PivotField for Values area | |
pivot.AddFieldToArea(PivotFieldType.Data, "amount"); | |
//Set the style of PivotTable | |
pivot.PivotTableStyleType = PivotTableStyleType.PivotTableStyleMedium9; | |
//Refresh and calculate data of PivotTable | |
pivot.RefreshData(); | |
pivot.CalculateData(); | |
book.Save("out.xlsm"); |
Online XLSM Editor Live Demos
Edit XLSM documents right now by visiting our Live Demos website . The live demo has the following benefits
XLSM What is XLSM File Format?
Files with XLSM extension is a type of Spreadsheet files that support Macros. From application point of view, a Macro is set of instructions that are used for automating processes. A macro is used to record the steps that are performed repeatedly and facilitates performing the actions by running the macro again. Macros are programmed with Microsoft's Visual Basic for Applications (VBA) from within the Excel Workbook using the Visual Basic Editor and can be run/debug directly from there.
Read More