MmsCore.Excel 0.1.2

There is a newer version of this package available.
See the version list below for details.
dotnet add package MmsCore.Excel --version 0.1.2                
NuGet\Install-Package MmsCore.Excel -Version 0.1.2                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="MmsCore.Excel" Version="0.1.2" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add MmsCore.Excel --version 0.1.2                
#r "nuget: MmsCore.Excel, 0.1.2"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install MmsCore.Excel as a Cake Addin
#addin nuget:?package=MmsCore.Excel&version=0.1.2

// Install MmsCore.Excel as a Cake Tool
#tool nuget:?package=MmsCore.Excel&version=0.1.2                

[!IMPORTANT] This Project is not yet stable. Breaking changes may occur at any time.

MmsCore.Excel

NuGet Version MmsCore.Excel NuGet Package Downloads TFM

MmsCore.Excelは、Excelファイルを読み書きするための効率的な機能を提供します。主なクラスには、ExcelReader、ExcelWriter、ExcelAccessorFactory、ExcelWorksheetReader、ExcelWorksheetWriterがあります。これらのクラスにより、Excelファイルの操作が容易になります。

🚀 Getting Started

IExcelReaderIExcelWorksheetReader

各セルの値を文字列として取得する場合のコード例を以下に示します。

// Excelファイルへのパスを設定します
const string xlsxFilePathToOpen = "UnitTest.xlsx";

// 一時的にスレッドのカルチャを日本語に設定します
using var temporaryThreadCulture = new TemporaryThreadCulture(new CultureInfo("ja-jp"));

// ExcelAccessorFactoryを利用してIExcelReaderのインスタンスを生成します
IExcelAccessorFactory factory = new ExcelAccessorFactory();
var reader = factory.CreateReader(xlsxFilePathToOpen);

// 指定したワークシートのリーダを取得します。存在しない場合はnullを返します
var worksheetReader = reader.GetWorksheetReaderOrNull("Sheet1");

// セル "C1"~"C4" の値を文字列として取得します
var c1 = worksheetReader?.GetStringOrNull("C1");
var c2 = worksheetReader?.GetStringOrNull("C2");
var c3 = worksheetReader?.GetStringOrNull("C3");
var c4 = worksheetReader?.GetStringOrNull("C4");

// worksheetReaderがnullでないこと、及び取得した各値が期待通りであることを検証します
Assert.NotNull(worksheetReader);
Assert.Equal("ABC", c1);
Assert.Equal("123", c2);
Assert.Equal("123.45", c3);
Assert.Equal("2022/12/31 0:00:00", c4);

各セルの値を特定の型として取得する場合のコード例を示します。

// Excelファイルへのパスを指定します
const string xlsxFilePathToOpen = "UnitTest.xlsx";

// 一時的にスレッドのカルチャを日本語に設定します
using var temporaryThreadCulture = new TemporaryThreadCulture(new CultureInfo("ja-jp"));

// ExcelAccessorFactoryを利用してIExcelReaderのインスタンスを生成します
IExcelAccessorFactory factory = new ExcelAccessorFactory();
var reader = factory.CreateReader(xlsxFilePathToOpen);

// 指定したワークシートのリーダを取得します。存在しない場合はnullを返します
var worksheetReader = reader.GetWorksheetReaderOrNull("Sheet1");

// カラム"C1"の値を文字列として取得します
var c1 = worksheetReader?.GetStringOrNull("C1");

// カラム"C2"の値をintとして取得します
var c2 = worksheetReader?.GetValueOrNull<int>("C2");

// カラム"C3"の値をdecimalとして取得します
var c3 = worksheetReader?.GetValueOrNull<decimal>("C3");

// カラム"C4"の値をDateTimeとして取得します
var c4 = worksheetReader?.GetValueOrNull<DateTime>("C4");

// worksheetReaderがnullでないこと、及び取得した各値が期待通りであることを検証します
Assert.NotNull(worksheetReader);
Assert.Equal("ABC", c1);
Assert.Equal(123, c2);
Assert.Equal(123.45m, c3);
Assert.Equal(DateTime.Parse("2022/12/31 0:00:00"), c4);

IExcelWriterIExcelWorksheetWriter

IExcelWriterIExcelWorksheetWriter の使用例を以下に示します。

// Excelファイルへのパスを設定します
const string xlsxFilePathToOpen = "UnitTest.xlsx";

// ExcelAccessorFactoryを利用してIExcelWriterのインスタンスを生成します
IExcelAccessorFactory factory = new ExcelAccessorFactory();
var writer = factory.CreateWriter(xlsxFilePathToOpen);

// ワークシートを用いて `IExcelWorksheetWriter` のインスタンスを生成します
var worksheetWriter = writer.GetWorksheetWriterOrNull("Sheet2");

// セルに値を設定します
var cellAddress = "A1";
var cellValue = "Hello, World!";
worksheetWriter?.SetCellValue(cellAddress, cellValue);

// 型付きのセル値も設定できます
var cellAddressInt = "B1";
var cellValueInt = 123;
worksheetWriter?.SetCellValue(cellAddressInt, cellValueInt);

var cellAddressDecimal = "C1";
var cellValueDecimal = 123.45m;
worksheetWriter?.SetCellValue(cellAddressDecimal, cellValueDecimal);

var cellAddressDateTime = "D1";
var cellValueDateTime = DateTime.Now;
worksheetWriter?.SetCellValue(cellAddressDateTime, cellValueDateTime);

// オフセットを指定してセルに値を設定します
var baseCellAddress = "A1";
var rowOffset = 1;
var columnOffset = 1;
worksheetWriter?.SetCellValue(baseCellAddress, rowOffset, columnOffset, "Offset Value");

// 行データを配列として設定します
worksheetWriter?.SetRowsDataArray(
    "A2",
    new[]
    {
        new { Name = "Alice", Age = 20, Birthday = new DateTime(2000, 1, 1) },
        new { Name = "Bob", Age = 30, Birthday = new DateTime(1990, 1, 1) },
    },
    (item, _) => item.Name,
    (item, _) => item.Age,
    (item, _) => item.Birthday);

// 書き込み結果のファイルを準備します
FileInfo fileInfo = new("UnitTest-Result.xlsx");
if (fileInfo.Exists)
{
    fileInfo.Delete();
}

// 書き込み結果をファイルとして保存します
using var stream = File.Open(fileInfo.FullName, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
writer.SaveTo(stream);

🙏 Acknowledgments

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.1.5 32 7/19/2024
0.1.4 1,216 5/4/2024
0.1.3 167 3/13/2024
0.1.2 213 2/29/2024
0.1.1 253 2/19/2024
0.1.0 134 2/12/2024