在生產環境中測試,無水印。
在任何需要的地方都能運行。
獲得 30 天的全功能產品。
在幾分鐘內上手運行。
試用產品期間完全訪問我們的支援工程團隊
在這個現代時代,我們需要一種更好的方式來在我們的 .NET Core 應用程式中處理 Excel 試算表。 在接下來的教程中,我們將學習如何在 .NET Core Excel 專案中存取電子表格並使用 C# 修改其值。
要在 .NET Core 中輕鬆處理 Excel 檔案,請嘗試使用 IronXL。 下載 IronXL DLL 或 使用 NuGet 安裝,免費用於開發項目。
Install-Package IronXL.Excel
現在您已下載 IronXL,讓我們開始吧。 在專案中載入 Excel 文件,然後訪問需要編輯和進行更改的WorkSheet
。
要編輯 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
使用(冒號:
)一次編輯多個單元格並分配靜態值非常容易。 其左側表示起始單元格,而右側則表示特定列中的最後一個單元格。
sheet [From:To]
這將把新值
從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
上述代碼將顯示以下輸出並從用戶獲取輸入:
具有使用者輸入的控制台應用程式 UI
在 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#的詳細信息和多個項目及代碼示例,請深入了解。