在实际环境中测试
在生产中测试无水印。
随时随地为您服务。
在这个现代化的时代,我们需要一种更好的方式在我们的 .NET Core 应用程序中使用 Excel 电子表格。 在下面的教程中,我们将学习如何使用 C# 访问 .NET Core Excel 项目中的电子表格并修改其值。
要在 .NET Core 中轻松处理 Excel 文件,请尝试使用 IronXL。 下载 IronXL DLL或使用 NuGet 安装供开发项目免费使用。
Install-Package IronXL.Excel
现在您已经下载了 IronXL,让我们开始吧。 在项目中加载 Excel 文件并访问工作表需要编辑和更改数据的地方。
要编辑 Excel 文件,请在项目中添加引用 IronXL
并通过 using 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# 文件使用本教程。