跳至页脚内容
使用 IRONXL

如何在 C# 中编辑电子表格

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.

How to Edit a Spreadsheet in C#

The ability to edit and manipulate the data of Excel files using C# can be extremely helpful to businesses and developers. The following is a brief overview of how this can be done with ease in five simple steps:

  1. Install the C# library for editing spreadsheets.
  2. Utilize the WorkBook.Load method to open an Excel file.
  3. Use the Worksheet.Value method to set the value of the cell.
  4. Apply the Range.Sum method to sum the values of the cells.
  5. Save the newly generated spreadsheet in .XLSX format using the SaveAs method.

IronXL

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 operations in C# development that can 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.

Create a New Visual Studio Project

Before installing IronXL, it is necessary 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.

  1. Open Visual Studio and click on the File menu, a drop-down menu will appear. In the dropdown menu, click on the New menu, and another side menu will appear. Click on Project.

    How to Edit a Spreadsheet in C#, Figure 1: Navigate to creating a new project in Visual Studio Navigate to creating a new project in Visual Studio

  2. 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 the C# option. After that, click on the Next button.

    How to Edit a Spreadsheet in C#, Figure 2: Create a new project dialog in Visual Studio Create a new project dialog in Visual Studio

  3. 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.

    How to Edit a Spreadsheet in C#, Figure 3: Configure the new project Configure the new project

  4. The last window will appear, in this window select the target framework and click on Create button.

    How to Edit a Spreadsheet in C#, Figure 4: Target Framework selection Target Framework selection

Installing Spreadsheet Library IronXL

Once the Project is created now let's install the free IronXL C# library, below are the steps to install IronXL in your C# project.

  1. In Visual Studio, go to Tools, a drop-down menu will appear. In the dropdown menu, click on the NuGet Package Manager, a side menu will appear. Click on the Manage NuGet Packages for Solution.

    How to Edit a Spreadsheet in C#, Figure 5: Navigate to NuGet Package Manager Navigate to NuGet Package Manager

  2. A new window will 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.

    How to Edit a Spreadsheet in C#, Figure 6: Install the IronXL package in the NuGet Package Manager Install the IronXL package in the NuGet Package Manager

Edit Excel Files Spreadsheets using IronXL

IronXL offers many spreadsheet manipulation features that allow users to change the values of cells, font size, background color of columns, cell ranges, and many more.

This section will demonstrate how to load a dataset from a spreadsheet Excel file, edit, then format, and manipulate them.

Changing Spreadsheet Entries Using C#

Below is the code example that changes Excel entries using C# programming language code with a few lines of code.

Input Excel File

How to Edit a Spreadsheet in C#, Figure 7: The input Excel file The input Excel file

using IronXL;

// Load an existing Excel file
WorkBook workBook = WorkBook.Load("test.xlsx");

// Get the default worksheet
WorkSheet workSheet = workBook.DefaultWorkSheet;

// Set the value of cell A1
workSheet["A1"].Value = "Hello World Created by IronXL";

// Save the modified workbook as a new file
workBook.SaveAs("sample.xlsx");
using IronXL;

// Load an existing Excel file
WorkBook workBook = WorkBook.Load("test.xlsx");

// Get the default worksheet
WorkSheet workSheet = workBook.DefaultWorkSheet;

// Set the value of cell A1
workSheet["A1"].Value = "Hello World Created by IronXL";

// Save the modified workbook as a new file
workBook.SaveAs("sample.xlsx");
Imports IronXL

' Load an existing Excel file
Private workBook As WorkBook = WorkBook.Load("test.xlsx")

' Get the default worksheet
Private workSheet As WorkSheet = workBook.DefaultWorkSheet

' Set the value of cell A1
Private workSheet("A1").Value = "Hello World Created by IronXL"

' Save the modified workbook as a new file
workBook.SaveAs("sample.xlsx")
$vbLabelText   $csharpLabel

The provided code utilizes the IronXL library in C# to 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 an XLSX file, "sample.xlsx", using the SaveAs method.

Output File

How to Edit a Spreadsheet in C#, Figure 8: The output file The output file

Adding Spreadsheet Cell Values Using Formulas

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;

// Load the existing Excel file
WorkBook workBook = WorkBook.Load("sample.xlsx");

// Get the default worksheet
WorkSheet workSheet = workBook.DefaultWorkSheet;

// Select a range of cells from D2 to D5
var range = workSheet["D2:D5"];

// Calculate the sum of the values in the range
decimal sum = range.Sum();

// Set the sum in cell D6 and label in C6
workSheet["D6"].Value = sum;
workSheet["C6"].Value = "Total Price:";

// Save the modified workbook
workBook.SaveAs("sample.xlsx");
using IronXL;

// Load the existing Excel file
WorkBook workBook = WorkBook.Load("sample.xlsx");

// Get the default worksheet
WorkSheet workSheet = workBook.DefaultWorkSheet;

// Select a range of cells from D2 to D5
var range = workSheet["D2:D5"];

// Calculate the sum of the values in the range
decimal sum = range.Sum();

// Set the sum in cell D6 and label in C6
workSheet["D6"].Value = sum;
workSheet["C6"].Value = "Total Price:";

// Save the modified workbook
workBook.SaveAs("sample.xlsx");
Imports IronXL

' Load the existing Excel file
Private workBook As WorkBook = WorkBook.Load("sample.xlsx")

' Get the default worksheet
Private workSheet As WorkSheet = workBook.DefaultWorkSheet

' Select a range of cells from D2 to D5
Private range = workSheet("D2:D5")

' Calculate the sum of the values in the range
Private sum As Decimal = range.Sum()

' Set the sum in cell D6 and label in C6
Private workSheet("D6").Value = sum
Private workSheet("C6").Value = "Total Price:"

' Save the modified workbook
workBook.SaveAs("sample.xlsx")
$vbLabelText   $csharpLabel

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.

Output File

How to Edit a Spreadsheet in C#, Figure 9: The output file The output file

Conclusion

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 spreadsheets, 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.

常见问题解答

如何在C#中操作电子表格而不使用Interop?

您可以使用IronXL库在C#中操作电子表格,而不依赖于Interop。IronXL允许您通过代码直接执行编辑、创建和格式化电子表格等任务。

在C#中编辑Excel电子表格的初始步骤是什么?

要在C#中编辑Excel电子表格,首先安装IronXL库,然后使用WorkBook.Load加载您的Excel文件。您可以使用Worksheet.Value修改单元格值,并使用SaveAs方法保存更改。

如何在Visual Studio项目中安装IronXL?

要在Visual Studio项目中安装IronXL,请转到工具 > NuGet包管理器 > 为解决方案管理NuGet包,搜索IronXL,选择最新版本,然后点击安装。

是什么让IronXL成为C#中强大的电子表格操作工具?

IronXL是C#中强大的电子表格操作工具,因为它允许您进行广泛的操作,如应用公式、转换文件格式、管理图表和处理图像,而无需Microsoft Excel。

如何在C#电子表格中进行单元格数值求和?

要在C#电子表格中使用IronXL求和单元格值,选择所需范围workSheet["D2:D5"],使用range.Sum()方法计算总和,并将结果分配给单元格workSheet["D6"].Value

使用C#库是否可以转换电子表格格式?

是的,使用IronXL,您可以轻松转换电子表格格式。该库支持各种Excel文件类型,并允许您将电子表格保存为.XLSX、.CSV等格式。

使用C#电子表格库的试用版本有什么好处?

IronXL的试用版本允许您探索其用于电子表格数据操作的广泛功能,帮助您在购买完整许可证之前评估其针对C#项目的能力。

我在哪里可以找到使用C#库进行电子表格操作的教程?

您可以在他们的官方网站上找到使用IronXL进行电子表格操作的教程和示例,特别是在他们的“操作指南”和“示例”部分,涵盖了各种功能和应用场景。

Curtis Chau
技术作家

Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。

除了开发之外,Curtis 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。