在实际环境中测试
在生产中测试无水印。
随时随地为您服务。
在当今时代,我们需要一种更好的方法在 .NET Core 应用程序中处理 Excel 电子表格。在下面的教程中,我们将学习如何使用 C# 访问 .NET Core Excel 项目中的电子表格并修改其值。
要在 .NET Core 中轻松处理 Excel 文件,请试试 IronXL。 下载 IronXL DLL 或 使用 NuGet 安装 供开发项目免费使用。
Install-Package IronXL.Excel
既然已经下载了 IronXL,那就让我们开始吧。在项目中加载 Excel 文件并访问 工作表 需要编辑和更改数据的地方。
要编辑 Excel 文件,请在项目中添加 "IronXL "引用,并通过 "使用 IronXL "导入库。
在下面的例子中,我们的 Excel 文件名是 sample.xlsx
,它存在于项目的 bin> Debug> netcoreapp3.1
文件夹中。我们将使用此代码编辑 sample.xlsx
中 A1
单元格中的 new value
值。
/**
Load WorkSheet
anchor-load-a-sample-file
**/
using IronXL;
static void Main(string [] args)
{
WorkBook wb = WorkBook.Load("sample.xlsx"); //load Excel file
WorkSheet ws = wb.GetWorkSheet("Sheet1"); //Get sheet1 of sample.xlsx
ws ["A1"].Value = "new value"; //access A1 cell and edit the value
wb.SaveAs("sample.xlsx"); //save changes
}
/**
Load WorkSheet
anchor-load-a-sample-file
**/
using IronXL;
static void Main(string [] args)
{
WorkBook wb = WorkBook.Load("sample.xlsx"); //load Excel file
WorkSheet ws = wb.GetWorkSheet("Sheet1"); //Get sheet1 of sample.xlsx
ws ["A1"].Value = "new value"; //access A1 cell and edit the value
wb.SaveAs("sample.xlsx"); //save changes
}
'''
'''Load WorkSheet
'''anchor-load-a-sample-file
'''*
Imports IronXL
Shared Sub Main(ByVal args() As String)
Dim wb As WorkBook = WorkBook.Load("sample.xlsx") 'load Excel file
Dim ws As WorkSheet = wb.GetWorkSheet("Sheet1") 'Get sheet1 of sample.xlsx
ws ("A1").Value = "new value" 'access A1 cell and edit the value
wb.SaveAs("sample.xlsx") 'save changes
End Sub
通过使用 (冒号).其左侧表示起始单元格,右侧表示特定列的最后一个单元格。
表格 [发件人:收件人]`
这将编辑 A 列A1
至A9
单元格中的新值
。
/**
Assign Value Multi Cells
anchor-assign-value-to-multiple-cells
**/
using IronXL;
static void Main(string [] args)
{
WorkBook wb = WorkBook.Load("sample.xlsx");
WorkSheet ws = wb.GetWorkSheet("Sheet1");
ws ["A1:A9"].Value = "new value";
wb.SaveAs("sample.xlsx");
}
/**
Assign Value Multi Cells
anchor-assign-value-to-multiple-cells
**/
using IronXL;
static void Main(string [] args)
{
WorkBook wb = WorkBook.Load("sample.xlsx");
WorkSheet ws = wb.GetWorkSheet("Sheet1");
ws ["A1:A9"].Value = "new value";
wb.SaveAs("sample.xlsx");
}
'''
'''Assign Value Multi Cells
'''anchor-assign-value-to-multiple-cells
'''*
Imports IronXL
Shared Sub Main(ByVal args() As String)
Dim wb As WorkBook = WorkBook.Load("sample.xlsx")
Dim ws As WorkSheet = wb.GetWorkSheet("Sheet1")
ws ("A1:A9").Value = "new value"
wb.SaveAs("sample.xlsx")
End Sub
下面是另一种情况,我们可以从用户那里获取值并编辑 Excel 文件。
/**
Edit Cells User Input
anchor-edit-cells-with-user-inputs
**/
using IronXL;
static void Main(string [] args)
{
string _from, _to, newValue ;
Console.Write("Enter Starting Cell :");
_from = Console.ReadLine();
Console.Write("Enter Last Cell :");
_to = Console.ReadLine();
Console.Write("Enter value:");
newValue = Console.ReadLine();
WorkBook wb = WorkBook.Load("sample.xlsx");
WorkSheet ws = wb.GetWorkSheet("Sheet1");
ws [_from + ":" + _to].Value = newValue;
wb.SaveAs("sample.xlsx");
Console.WriteLine("Successfully Changed...!");
Console.ReadKey();
}
/**
Edit Cells User Input
anchor-edit-cells-with-user-inputs
**/
using IronXL;
static void Main(string [] args)
{
string _from, _to, newValue ;
Console.Write("Enter Starting Cell :");
_from = Console.ReadLine();
Console.Write("Enter Last Cell :");
_to = Console.ReadLine();
Console.Write("Enter value:");
newValue = Console.ReadLine();
WorkBook wb = WorkBook.Load("sample.xlsx");
WorkSheet ws = wb.GetWorkSheet("Sheet1");
ws [_from + ":" + _to].Value = newValue;
wb.SaveAs("sample.xlsx");
Console.WriteLine("Successfully Changed...!");
Console.ReadKey();
}
'''
'''Edit Cells User Input
'''anchor-edit-cells-with-user-inputs
'''*
Imports IronXL
Shared Sub Main(ByVal args() As String)
Dim _from, _to, newValue As String
Console.Write("Enter Starting Cell :")
_from = Console.ReadLine()
Console.Write("Enter Last Cell :")
_to = Console.ReadLine()
Console.Write("Enter value:")
newValue = Console.ReadLine()
Dim wb As WorkBook = WorkBook.Load("sample.xlsx")
Dim ws As WorkSheet = wb.GetWorkSheet("Sheet1")
ws (_from & ":" & _to).Value = newValue
wb.SaveAs("sample.xlsx")
Console.WriteLine("Successfully Changed...!")
Console.ReadKey()
End Sub
上述代码将显示以下输出并接受用户输入:
带有用户输入的控制台应用程序用户界面
在 ExcelSheet 中,我们可以看到数值从 B4 变为 B9:
新值从 B4 填至 B9
编辑多个单元格并分配动态值非常简单。让我们看看下面的示例:
/**
Edit Multi Cells Static Value
anchor-edit-multiple-cells-with-static-value
**/
using IronXL;
static void Main(string [] args)
{
WorkBook wb = WorkBook.Load("sample.xlsx");
WorkSheet ws = wb.GetWorkSheet("Sheet1");
for (int i = From; i <= To; i++) //Set cell range of column A to be edit.
{
ws ["A" + i].Value = "Value"+i;
}
wb.SaveAs("sample.xlsx");
}
/**
Edit Multi Cells Static Value
anchor-edit-multiple-cells-with-static-value
**/
using IronXL;
static void Main(string [] args)
{
WorkBook wb = WorkBook.Load("sample.xlsx");
WorkSheet ws = wb.GetWorkSheet("Sheet1");
for (int i = From; i <= To; i++) //Set cell range of column A to be edit.
{
ws ["A" + i].Value = "Value"+i;
}
wb.SaveAs("sample.xlsx");
}
'''
'''Edit Multi Cells Static Value
'''anchor-edit-multiple-cells-with-static-value
'''*
Imports IronXL
Shared Sub Main(ByVal args() As String)
Dim wb As WorkBook = WorkBook.Load("sample.xlsx")
Dim ws As WorkSheet = wb.GetWorkSheet("Sheet1")
For i As Integer = From To [To] 'Set cell range of column A to be edit.
ws ("A" & i).Value = "Value" & i
Next i
wb.SaveAs("sample.xlsx")
End Sub
深入了解更多详细信息以及多个项目和代码示例,如果您想进一步了解如何 读取 Excel C# 文件 使用本教程。