.NET CoreでExcelを操作する
.NET Core Excel 概要
この現代の時代において、私たちは .NET Core アプリケーションで Excel スプレッドシートを扱うためのより良い方法が必要です。 以下のチュートリアルでは、.NET Core Excel プロジェクトでスプレッドシートにアクセスし、C# を使用して値を変更する方法を学びます。
.NET Core Excel編集
- IronXLライブラリをダウンロードする
- セル範囲に値を割り当てる
- ユーザー入力を使用してセルを編集する
- 静的な値を使用して複数のセルを編集する
ステップ1
1. IronXL ライブラリをダウンロード
.NET Core で Excel ファイルを簡単に扱うには、IronXL を試してください。 IronXL DLL をダウンロード または NuGet でインストール して開発プロジェクトで無料で使用してください。
# Install IronXL using the .NET CLI
dotnet add package IronXL.Excel# Install IronXL using the .NET CLI
dotnet add package IronXL.Excelチュートリアル
2. .NET Core Excel 編集プロジェクト
IronXL をダウンロードしたので、始めましょう。 プロジェクトに Excel ファイルを読み込み、データを編集し変更を加える必要がある WorkSheet にアクセスします。
3. 特定のセル値を編集
Excel ファイルを編集するには、プロジェクトに IronXL の参照を追加し、using IronXL でライブラリをインポートします。
3.1. サンプルファイルを読み込む
次の場合、Excel ファイルの名前は sample.xlsx であり、プロジェクトの bin> Debug> netcoreapp3.1 フォルダに存在します。 このコードを使用して sample.xlsx のセル A1 に new value を編集します。
// Anchor: Load a sample file
using IronXL;
static void Main(string[] args)
{
// Load the Excel workbook
WorkBook wb = WorkBook.Load("sample.xlsx");
// Get the first worksheet named "Sheet1"
WorkSheet ws = wb.GetWorkSheet("Sheet1");
// Access cell A1 and set its value to "new value"
ws["A1"].Value = "new value";
// Save the changes to the Excel workbook
wb.SaveAs("sample.xlsx");
}// Anchor: Load a sample file
using IronXL;
static void Main(string[] args)
{
// Load the Excel workbook
WorkBook wb = WorkBook.Load("sample.xlsx");
// Get the first worksheet named "Sheet1"
WorkSheet ws = wb.GetWorkSheet("Sheet1");
// Access cell A1 and set its value to "new value"
ws["A1"].Value = "new value";
// Save the changes to the Excel workbook
wb.SaveAs("sample.xlsx");
}' Anchor: Load a sample file
Imports IronXL
Shared Sub Main(ByVal args() As String)
' Load the Excel workbook
Dim wb As WorkBook = WorkBook.Load("sample.xlsx")
' Get the first worksheet named "Sheet1"
Dim ws As WorkSheet = wb.GetWorkSheet("Sheet1")
' Access cell A1 and set its value to "new value"
ws("A1").Value = "new value"
' Save the changes to the Excel workbook
wb.SaveAs("sample.xlsx")
End Sub4. 複数のセルに値を割り当てる
複数のセルを編集して静的な値を一度に割り当てるのは非常に簡単です。コロン : を使用します。 その左側は開始セルを示し、右側は特定の列の最後のセルを示します。
sheet[From:To]
これにより、列 A のセル A1 から A9 まで new value が編集されます。
// Anchor: Assign Value to Multiple Cells
using IronXL;
static void Main(string[] args)
{
WorkBook wb = WorkBook.Load("sample.xlsx");
WorkSheet ws = wb.GetWorkSheet("Sheet1");
// Set the value "new value" for cells from A1 to A9
ws["A1:A9"].Value = "new value";
wb.SaveAs("sample.xlsx");
}// Anchor: Assign Value to Multiple Cells
using IronXL;
static void Main(string[] args)
{
WorkBook wb = WorkBook.Load("sample.xlsx");
WorkSheet ws = wb.GetWorkSheet("Sheet1");
// Set the value "new value" for cells from A1 to A9
ws["A1:A9"].Value = "new value";
wb.SaveAs("sample.xlsx");
}' 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")
' Set the value "new value" for cells from A1 to A9
ws("A1:A9").Value = "new value"
wb.SaveAs("sample.xlsx")
End Sub5. ユーザー入力でセルを編集
ユーザーから値を取得して Excel ファイルを編集する別のケースです。
// Anchor: Edit Cells with User Inputs
using System;
using IronXL;
static void Main(string[] args)
{
string _from, _to, newValue;
// Capture user inputs
Console.Write("Enter Starting Cell: ");
_from = Console.ReadLine();
Console.Write("Enter Last Cell: ");
_to = Console.ReadLine();
Console.Write("Enter value: ");
newValue = Console.ReadLine();
// Load the Excel workbook and access the worksheet
WorkBook wb = WorkBook.Load("sample.xlsx");
WorkSheet ws = wb.GetWorkSheet("Sheet1");
// Assign the user-entered value to the specified cell range
ws[_from + ":" + _to].Value = newValue;
// Save changes to the workbook
wb.SaveAs("sample.xlsx");
Console.WriteLine("Successfully Changed...!");
Console.ReadKey();
}// Anchor: Edit Cells with User Inputs
using System;
using IronXL;
static void Main(string[] args)
{
string _from, _to, newValue;
// Capture user inputs
Console.Write("Enter Starting Cell: ");
_from = Console.ReadLine();
Console.Write("Enter Last Cell: ");
_to = Console.ReadLine();
Console.Write("Enter value: ");
newValue = Console.ReadLine();
// Load the Excel workbook and access the worksheet
WorkBook wb = WorkBook.Load("sample.xlsx");
WorkSheet ws = wb.GetWorkSheet("Sheet1");
// Assign the user-entered value to the specified cell range
ws[_from + ":" + _to].Value = newValue;
// Save changes to the workbook
wb.SaveAs("sample.xlsx");
Console.WriteLine("Successfully Changed...!");
Console.ReadKey();
}' Anchor: Edit Cells with User Inputs
Imports System
Imports IronXL
Shared Sub Main(ByVal args() As String)
Dim _from, _to, newValue As String
' Capture user inputs
Console.Write("Enter Starting Cell: ")
_from = Console.ReadLine()
Console.Write("Enter Last Cell: ")
_to = Console.ReadLine()
Console.Write("Enter value: ")
newValue = Console.ReadLine()
' Load the Excel workbook and access the worksheet
Dim wb As WorkBook = WorkBook.Load("sample.xlsx")
Dim ws As WorkSheet = wb.GetWorkSheet("Sheet1")
' Assign the user-entered value to the specified cell range
ws(_from & ":" & _to).Value = newValue
' Save changes to the workbook
wb.SaveAs("sample.xlsx")
Console.WriteLine("Successfully Changed...!")
Console.ReadKey()
End Sub上記のコードはユーザー入力用のコンソールを表示し、指定された Excel セルの値を入力された値で更新します。
ユーザー入力があるコンソールアプリケーション UI
ExcelSheet の B4 から B9 に変更された値が見られるように:
B4 から B9 に新しい値が埋め込まれています
6. 静的値で複数のセルを編集
複数のセルを編集して動的な値を割り当てるのは非常に簡単です。 次の例を見てみましょう。
// 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");
// Ensure 'from' and 'to' are defined for the intended cell range
int from = 1;
int to = 9;
// Iterate over a range of cells and update them with dynamic values
for (int i = from; i <= to; i++)
{
ws["A" + i].Value = "Value" + i;
}
// Save the changes to the Excel file
wb.SaveAs("sample.xlsx");
}// 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");
// Ensure 'from' and 'to' are defined for the intended cell range
int from = 1;
int to = 9;
// Iterate over a range of cells and update them with dynamic values
for (int i = from; i <= to; i++)
{
ws["A" + i].Value = "Value" + i;
}
// Save the changes to the Excel file
wb.SaveAs("sample.xlsx");
}' 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")
' Ensure 'from' and 'to' are defined for the intended cell range
Dim from As Integer = 1
Dim [to] As Integer = 9
' Iterate over a range of cells and update them with dynamic values
For i As Integer = From To [to]
ws("A" & i).Value = "Value" & i
Next i
' Save the changes to the Excel file
wb.SaveAs("sample.xlsx")
End Sub7. Excel ファイルを詳細に読むチュートリアル
このチュートリアルで C# で Excel ファイルを読む方法 についてより詳細を学ぶために、さらなる詳細と複数のプロジェクトとコード例に進んでください。






