在實際環境中測試
在生產環境中測試無浮水印。
在任何需要的地方都能運作。
在這個現代化時代,我們需要一種更好的方式來在我們的 .NET Core 應用程式中操作 Excel 試算表。在以下的教學中,我們將學習如何在 .NET Core Excel 專案中訪問試算表並使用 C# 修改其值。
如果您需要在 .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
您可以輕鬆地編輯多個儲存格,並同時賦值靜態值,通過使用 (冒號 :
). 左側表示特定列的起始單元格,右側表示該列的最後一個單元格。
sheet [從:到]這將編輯單元格
A1到
A9的
欄 A中的
new value`。
/**
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
Excel工作表中 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# 透過本教程。