Test in a live environment
Test in production without watermarks.
Works wherever you need it to.
This article will explore the art of spreadsheet manipulation in C#, with the help of the most advanced C# data manipulating library named IronXL without interacting with the user interface.
The ability to edit and manipulate the data of Excel files using C# can be extremely helpful to businesses and developers, the following steps are a brief overview of how this can be done with ease in five simple steps
WorkBook.Load
method to open an Excel file.Worksheet.Value
method to set the value of the sell.Range.Sum
method to sum the values of the cells.SaveAs
method.IronXL is a powerful tool that has become a game-changer in the realm of C# spreadsheet processing. As a comprehensive library designed specifically for C#.
Whether you're working on a financial application, data-driven dashboard, or any project that involves handling tabular data, IronXL provides a robust and user-friendly solution that simplifies complex spreadsheet operatios in C# development that easily be implemented in your Web Application or your .NET applications.
This article will explore the key features and benefits of IronXL, shedding light on how it enhances the efficiency and functionality of C# applications that deal with Excel spreadsheet processing. Using IronXL you can manipulate Excel spreadsheets without the need for Microsoft Office applications, also you can create advanced Excel spreadsheets without Microsoft Excel and Excel interop.
Before installing IronXL, It is nescessary to create a new Visual Studio C# project or load an existing one, below are the steps to create a new project in Visual Studio.
Open Visual Studio and click on the File menu, there a drop-down menu will appear, in the dropdown menu click on the New menu, and another side menu will appear, click on Project.
Navigate to creating a new project in Visual Studio
A new window will appear in this new window click on the search bar and write Console Application in the search bar and select the one with C# option, after that click on Next button.
Create a new project dialog in Visual Studio
A new configuration window will open, in this new window write the project Name, set the project location and then click on the Next button.
Configure the new project
The last window will appear, in this window select the target framework and click on Create button.
Target Framework selection
Once the Project is created now let's install the free IronXL C# library, below are the steps to install IronXL in your C#
In the Visual Studio go to Tools, a drop-down menu will appear in the dropdown menu click on the NuGet Package Manager, and a new side menu will appear in the side menu by clicking on the Manage NuGet Packages for Solution.
Navigate to NuGet Package Manager
A new window in appear in this new window go to the browser tab and in the search bar write IronXL, a list of IronXL Packages will appear, select the latest package and click on install.
Install the IronXL package in the NuGet Package Manager
IronXL offers many spreadsheet manipulation features that allow users to change the values of cells, font size, background color of columns, cell range and many more.
This section will demonstrate how to load a data set from a spreadsheet Excel file and edit then format and manipulate them.
Below is the code example that changes Excel entries using C# programming language code with a few lines of code.
The input Excel file
using IronXL;
WorkBook workBook = WorkBook.Load("test.xlsx");
WorkSheet workSheet = workBook.DefaultWorkSheet;
workSheet["A1"].Value = "Hello World Created by IronXL";
workBook.SaveAs("sample.xlsx");
using IronXL;
WorkBook workBook = WorkBook.Load("test.xlsx");
WorkSheet workSheet = workBook.DefaultWorkSheet;
workSheet["A1"].Value = "Hello World Created by IronXL";
workBook.SaveAs("sample.xlsx");
Imports IronXL
Private workBook As WorkBook = WorkBook.Load("test.xlsx")
Private workSheet As WorkSheet = workBook.DefaultWorkSheet
Private workSheet("A1").Value = "Hello World Created by IronXL"
workBook.SaveAs("sample.xlsx")
The provided code utilizes the IronXL library in C# to open source manipulate spreadsheets in Excel files. It begins by importing the IronXL namespace. Then, it loads an existing Excel file named "test.xlsx" into a WorkBook
object.
The default worksheet of this workbook is accessed and assigned to a WorkSheet
variable, denoted as "workSheet". The code sets the value of cell A1 in the worksheet to "Hello World Created by IronXL". Finally, the modified Spreadsheet is saved as XLSX files, for example "sample.xlsx", which will be saved using the SaveAs
method.
The output file
IronXL offers a free version of this feature where you can use formulas on spreadsheets and write them to other cells, below is the example using C# IronXL Spreadsheet Library.
using IronXL;
WorkBook workBook = WorkBook.Load("sample.xlsx");
WorkSheet workSheet = workBook.DefaultWorkSheet;
var range = workSheet["D2:D5"];
decimal sum = range.Sum();
workSheet["D6"].Value = sum;
workSheet["C6"].Value = "Total Price:";
workBook.SaveAs("sample.xlsx");
using IronXL;
WorkBook workBook = WorkBook.Load("sample.xlsx");
WorkSheet workSheet = workBook.DefaultWorkSheet;
var range = workSheet["D2:D5"];
decimal sum = range.Sum();
workSheet["D6"].Value = sum;
workSheet["C6"].Value = "Total Price:";
workBook.SaveAs("sample.xlsx");
Imports IronXL
Private workBook As WorkBook = WorkBook.Load("sample.xlsx")
Private workSheet As WorkSheet = workBook.DefaultWorkSheet
Private range = workSheet("D2:D5")
Private sum As Decimal = range.Sum()
Private workSheet("D6").Value = sum
Private workSheet("C6").Value = "Total Price:"
workBook.SaveAs("sample.xlsx")
This code employs the IronXL library in C# to manipulate an Excel file named "sample.xlsx." It begins by loading the existing Excel file into a WorkBook
object.
The default worksheet of this workbook is accessed and assigned to a WorkSheet
variable, denoted as "workSheet". Subsequently, a specific range of cells, from D2 to D5, is selected using the workSheet["D2:D5"]
notation.
The sum of the values within this range is then calculated using the Sum
method and stored in a variable named "sum".
The code updates cell D6 in the new worksheet with this calculated sum and cell C6 is assigned the label "Total Price:". Finally, the modified workbook is saved back to the same file, "sample.xlsx," using the SaveAs
method.
The output file
This article has explored the significance of C# in spreadsheet manipulation and introduced IronXL as a powerful library that enhances the capabilities of C#.
This tutorial discussed the process of creating a new Visual Studio C# project and outlined the steps to install IronXL. The article also delved into practical examples of using IronXL to edit Excel files, demonstrating how to change cell values and apply formulas to spreadsheet data.
IronXL proves to be a valuable tool for developers working on projects involving data analysis, reporting, and support for other spreadsheet-related functionalities, providing a seamless and efficient solution for exporting data used for C# applications.
Furthermore, IronXL also offers a wide range of features to interact with Excel WorkBook, WorkSheet and Cells level such as converting between popular formats, cell data formatting, merging cells, inserting math functions, and even managing charts and adding images.
The sample code and example of using formulas in spreadsheets can be found in this example. For the complete tutorial on editing spreadsheet visit the following how-to.
Opt into IronXL's trial today to begin exploring all of its features, and see how IronXL can help benefit your projects, whether you want to create a new Excel file, edit existing worksheets, or add new cell data to a spreadsheet or cell range.
If you find IronXL to be beneficial to your working environment and wish to continue using the features it has to offer for all your spreadsheet data manipulation needs and more, you can purchase a license once your trial ends.
9 .NET API products for your office documents