C#およびVB.NETアプリケーションでのExcelスプレッドシートファイル

This article was translated from English: Does it need improvement?
Translated
View the article in English

Excelの読み書き (XLS、XLSX、CSV) C# およびその他のすべての .NET 言語でファイルを操作することは、Iron Software の IronXL ソフトウェア ライブラリを使用することで簡単です。

IronXLは、サーバーにExcelをインストールしたり、Interopを使用したりする必要がありません。IronXLは、Microsoft.Office.Interop.Excelよりも高速で直感的なAPIを提供します。

IronXLは以下のプラットフォームで動作します:

  • WindowsおよびAzureのための.NET Framework 4.6.2以上
  • Windows、Linux、MacOS、およびAzure向けの.NET Core 2以降

  • .NET 5、.NET 6、.NET 7、.NET 8、Mono、モバイル、Xamarin

IronXLをインストール

まず、NuGetパッケージを使用してIronXLをインストールするか、 DLLをダウンロードする. IronXL クラスは以下にあります IronXL 名前空間。

IronXLをインストールする最も簡単な方法は、Visual-StudioのNuGetパッケージマネージャーを使用することです。

パッケージ名はIronXL.Excelです。

Install-Package IronXL.Excel

https://www.nuget.org/packages/ironxl.excel/

Excelドキュメントの読み取り

IronXLを使用してExcelファイルからデータを読み取るには、数行のコードが必要です。

:path=/static-assets/excel/content-code-examples/get-started/get-started-1.cs
using IronXL;

// Supported spreadsheet formats for reading include: XLSX, XLS, CSV and TSV
WorkBook workBook = WorkBook.Load("data.xlsx");
WorkSheet workSheet = workBook.WorkSheets.First();

// Select cells easily in Excel notation and return the calculated value, date, text or formula
int cellValue = workSheet["A2"].IntValue;

// Read from Ranges of cells elegantly.
foreach (var cell in workSheet["A2:B10"])
{
    Console.WriteLine("Cell {0} has value '{1}'", cell.AddressString, cell.Text);
}
Imports IronXL

' Supported spreadsheet formats for reading include: XLSX, XLS, CSV and TSV
Private workBook As WorkBook = WorkBook.Load("data.xlsx")
Private workSheet As WorkSheet = workBook.WorkSheets.First()

' Select cells easily in Excel notation and return the calculated value, date, text or formula
Private cellValue As Integer = workSheet("A2").IntValue

' Read from Ranges of cells elegantly.
For Each cell In workSheet("A2:B10")
	Console.WriteLine("Cell {0} has value '{1}'", cell.AddressString, cell.Text)
Next cell
VB   C#

新しいExcelドキュメントの作成

C#またはVB.NETでExcelドキュメントを作成するには IronXLはシンプルで高速なインターフェースを提供します。

:path=/static-assets/excel/content-code-examples/get-started/get-started-2.cs
using IronXL;

// Create new Excel WorkBook document.
WorkBook workBook = WorkBook.Create(ExcelFileFormat.XLSX);
workBook.Metadata.Author = "IronXL";

// Add a blank WorkSheet
WorkSheet workSheet = workBook.CreateWorkSheet("main_sheet");

// Add data and styles to the new worksheet
workSheet["A1"].Value = "Hello World";
workSheet["A2"].Style.BottomBorder.SetColor("#ff6600");
workSheet["A2"].Style.BottomBorder.Type = IronXL.Styles.BorderType.Double;

// Save the excel file
workBook.SaveAs("NewExcelFile.xlsx");
Imports IronXL

' Create new Excel WorkBook document.
Private workBook As WorkBook = WorkBook.Create(ExcelFileFormat.XLSX)
workBook.Metadata.Author = "IronXL"

' Add a blank WorkSheet
Dim workSheet As WorkSheet = workBook.CreateWorkSheet("main_sheet")

' Add data and styles to the new worksheet
workSheet("A1").Value = "Hello World"
workSheet("A2").Style.BottomBorder.SetColor("#ff6600")
workSheet("A2").Style.BottomBorder.Type = IronXL.Styles.BorderType.Double

' Save the excel file
workBook.SaveAs("NewExcelFile.xlsx")
VB   C#

CSV、XLS、XLSX、JSON、またはXMLとしてエクスポート

さまざまな一般的な構造化スプレッドシート形式で保存またはエクスポートすることもできます。

:path=/static-assets/excel/content-code-examples/get-started/get-started-3.cs
// Export to many formats with fluent saving
workSheet.SaveAs("NewExcelFile.xls");
workSheet.SaveAs("NewExcelFile.xlsx");
workSheet.SaveAsCsv("NewExcelFile.csv");
workSheet.SaveAsJson("NewExcelFile.json");
workSheet.SaveAsXml("NewExcelFile.xml");
' Export to many formats with fluent saving
workSheet.SaveAs("NewExcelFile.xls")
workSheet.SaveAs("NewExcelFile.xlsx")
workSheet.SaveAsCsv("NewExcelFile.csv")
workSheet.SaveAsJson("NewExcelFile.json")
workSheet.SaveAsXml("NewExcelFile.xml")
VB   C#

セルおよび範囲のスタイリング

Excelのセルと範囲は、IronXL.Range.Styleオブジェクトを使用してスタイル設定できます。

:path=/static-assets/excel/content-code-examples/get-started/get-started-4.cs
// Set cell's value and styles
workSheet["A1"].Value = "Hello World";
workSheet["A2"].Style.BottomBorder.SetColor("#ff6600");
workSheet["A2"].Style.BottomBorder.Type = IronXL.Styles.BorderType.Double;
' Set cell's value and styles
workSheet("A1").Value = "Hello World"
workSheet("A2").Style.BottomBorder.SetColor("#ff6600")
workSheet("A2").Style.BottomBorder.Type = IronXL.Styles.BorderType.Double
VB   C#

範囲の並べ替え

IronXL を使用すると、Range を使って Excel セルの範囲を並べ替えることができます。

:path=/static-assets/excel/content-code-examples/get-started/get-started-5.cs
using IronXL;

WorkBook workBook = WorkBook.Load("test.xls");
WorkSheet workSheet = workBook.WorkSheets.First();

// This is how we get range from Excel worksheet
Range range = workSheet["A2:A8"];

// Sort the range in the sheet
range.SortAscending();
workBook.Save();
Imports IronXL

Private workBook As WorkBook = WorkBook.Load("test.xls")
Private workSheet As WorkSheet = workBook.WorkSheets.First()

' This is how we get range from Excel worksheet
Private range As Range = workSheet("A2:A8")

' Sort the range in the sheet
range.SortAscending()
workBook.Save()
VB   C#

数式の編集

Excelの数式を編集するのは、最初に = イコール記号を付けて値を割り当てるのと同じくらい簡単です。 計算式はリアルタイムで計算されます。

:path=/static-assets/excel/content-code-examples/get-started/get-started-6.cs
// Set a formula
workSheet["A1"].Value = "=SUM(A2:A10)";

// Get the calculated value
decimal sum = workSheet["A1"].DecimalValue;
' Set a formula
workSheet("A1").Value = "=SUM(A2:A10)"

' Get the calculated value
Dim sum As Decimal = workSheet("A1").DecimalValue
VB   C#

なぜIronXLを選ぶべきですか?

IronXLは、.NET向けのエクセルドキュメントを読み書きするための簡単なAPIを開発者に提供します。

IronXLは、ExcelドキュメントにアクセスするためにサーバーにMicrosoft Excelをインストールする必要も、Excel Interopを使用する必要もありません。 これにより、.NET での Excel ファイルの操作が非常に迅速かつ簡単になります。

これから進む

IronXLの機能を最大限に活用するために、ドキュメントをお読みいただくことをお勧めします。 .NET APIリファレンス MSDN形式で。