In this step-by-step tutorial, we will learn how to edit Excel file in C#. As it is not always feasible to edit Excel files using MS Excel manually, therefore you may require to update Excel file in C# such that its data and formulas are updated and saved in the same Excel file. Following are the steps to explain how in C# update Excel file and save it on the disk.
Steps to Edit Excel File in C#
- Using NuGet package manager, add Aspose.Cells for .NET to the project
- Add a reference to Aspose.Cells namespace
- Load the workbook containing data and formula
- Get the references of the cells whose data and formulas are to be updated
- Update data and formula in the target cells
- Calculate the workbook to update formula result
- Save the workbook
The above process describes how in C# edit Excel file programmatically and save on disk. First, we open the Excel file then we get references to the target cells where data or formulas are to be updated. In the next step put the desired value or formula and re-calculate the workbook to calculate the formula results. You can not only update data or formulas but can also update pivot tables, slicers, charts, tables, drawing objects, formatting, and other workbook properties.
Code to Update Excel File in C#
This entire process neither requires MS Excel nor interop for updating the workbook. You can perform many other operations as well like if you want to create a chart, refer to how to create Excel Pie chart in C#.