ExcelVBA
Sub Sample()
Dim AddSheetObj As Worksheet
Dim BookObj As Workbook
Dim FilePath As String
Dim SheetCount As Long
Dim LastSheetObj As Worksheet
'ファイルパス作成
FilePath = ThisWorkbook.Path & "\sample.xlsx"
'ブックを開く
Set BookObj = Workbooks.Open(FilePath)
'シート数を取得
SheetCount = BookObj.Sheets.Count
'最後のシートを取得
Set LastSheetObj = BookObj.Sheets(SheetCount)
'渡し、その後ろに追加するよう指示する
Set AddSheetObj = BookObj.Sheets.Add(After:=LastSheetObj)
'追加されたシートの名前を変える
AddSheetObj.Name = "sample"
'保存
BookObj.Save
'閉じる
BookObj.Close
Set BookObj = Nothing
End Sub
Excel(COM)
using System;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using Excel = Microsoft.Office.Interop.Excel;
namespace SampleCode; // C#10~
internal class SampleExcel
{
static void Main(string[] args)
{
Excel.Application? application = null;
Excel.Workbooks? workbooks = null;
Excel.Workbook? workbook = null;
Excel.Sheets? worksheets = null;
Excel.Worksheet? addWorksheet = null;
Excel.Worksheet? lastWorksheet = null;
try
{
// 実行ファイルのあるフォルダパス取得
var folderPath = Path.GetDirectoryName(Assembly.GetEntryAssembly()?.Location);
if (folderPath == null) { return; }
// Excelファイルパスを作成
var filePath = Path.Combine(folderPath, "sample.xlsx");
// Excelを開く
application = new Excel.Application();
application.Visible = true;
// ブックを開く
workbooks = application.Workbooks;
workbook = workbooks.Open(filePath);
// シートを取得
worksheets = workbook.Sheets;
// 最後のシートを取得
var sheetCount = worksheets.Count;
lastWorksheet = worksheets[sheetCount];
// シート追加
addWorksheet = worksheets.Add(After: lastWorksheet);
// シート名変更
addWorksheet.Name = "sample";
// 保存
workbook.Save();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
CleanUpComObject(ref addWorksheet);
CleanUpComObject(ref lastWorksheet);
CleanUpComObject(ref worksheets);
CleanUpComObject(ref workbook, true, true);
CleanUpComObject(ref workbooks);
CleanUpComObject(ref application);
}
}
static void CleanUpComObject<T>(ref T comObject, bool shouldClose = true, bool saveChanges = false)
{
// フラグによってWorkbookはClose / ApplicationはQuitする
if (shouldClose && comObject != null)
{
// 型をチェックする
if (comObject is Microsoft.Office.Interop.Excel.Workbook workbook)
{
// Workbookの場合
workbook.Close(saveChanges);
}
else if (comObject is Microsoft.Office.Interop.Excel.Application application)
{
// Applicationの場合
application.Quit();
}
}
// Objectを解放
if (comObject != null && Marshal.IsComObject(comObject))
{
Marshal.ReleaseComObject(comObject);
comObject = default!;
}
}
}
ClosedXML
using ClosedXML.Excel;
using System;
using System.IO;
using System.Reflection;
namespace SampleCode; // C#10~
internal class SampleClosedXML
{
static void Main(string[] args)
{
try
{
// 実行ファイルのあるフォルダパス取得
var folderPath = Path.GetDirectoryName(Assembly.GetEntryAssembly()?.Location);
if (folderPath == null) { return; }
// Excelファイルパスを作成
var filePath = Path.Combine(folderPath, "sample.xlsx");
// ブックを開く、読み書きモード、共有なし(排他)
using (var stream = new FileStream(filePath, FileMode.Open, FileAccess.ReadWrite, FileShare.None))
using (var workbook = new XLWorkbook(stream))
{
// 一番最後にsampleシート追加
workbook.Worksheets.Add("sample");
// 上書き保存
workbook.Save();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
EPPlus
using OfficeOpenXml;
using System;
using System.IO;
using System.Reflection;
namespace SampleCode; // C#10~
internal class SampleEPPlus
{
static void Main(string[] args)
{
// Ver8.0のソースです
// 非商用個人利用の場合 名前を設定
ExcelPackage.License.SetNonCommercialPersonal("SampleTarou");
try
{
// 実行ファイルのあるフォルダパス取得
var folderPath = Path.GetDirectoryName(Assembly.GetEntryAssembly()?.Location);
if (folderPath == null) { return; }
// Excelファイルパスを作成
var filePath = Path.Combine(folderPath, "sample.xlsx");
// メモリにすべて取り込む。これなら遅延でStreamにアクセスするような問題が発生しない
byte[] fileBytes = File.ReadAllBytes(filePath);
using (var readStream = new MemoryStream(fileBytes))
using (var package = new ExcelPackage(readStream))
{
// シートを追加
var worksheet = package.Workbook.Worksheets.Add("sample");
// あらためて書き込み用としてStreamを開く
using (var writeStream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None))
{
// 書き込み
package.SaveAs(writeStream);
}
}
}
catch (Exception ex)
{
Console.WriteLine($"エラー: {ex.Message}");
}
}
}
ClosedXMLだと、直接Streamを使って上書きしているのに対し、
EPPlusだと、別のStreamに保存し元のStreamを空にしてからコピーをする手段をとっています
ライブラリ内部の設計の違いからくるもののそうです
ExcelDataReader
読み取りのみのためなし
NPOI
□xls
using NPOI.HSSF.UserModel;
using System;
using System.IO;
using System.Reflection;
namespace SampleCode; // C#10~
internal class SampleNPOI_xls
{
static void Main(string[] args)
{
try
{
// 実行ファイルのあるフォルダパス取得
var folderPath = Path.GetDirectoryName(Assembly.GetEntryAssembly()?.Location);
if (folderPath == null) { return; }
// Excelファイルパスを作成
var filePath = Path.Combine(folderPath, "sample.xls");
// メモリにすべて取り込む。これなら遅延でStreamにアクセスするような問題が発生しない
byte[] fileBytes = File.ReadAllBytes(filePath);
using (var readStream = new MemoryStream(fileBytes))
using (var workbook = new HSSFWorkbook(readStream))
{
// シートを追加
workbook.CreateSheet("sample");
// あらためて書き込み用としてStreamを開く
using (var writeStream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None))
{
// 書き込み
workbook.Write(writeStream);
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
□xlsx
using NPOI.XSSF.UserModel;
using System;
using System.IO;
using System.Reflection;
namespace SampleCode; // C#10~
internal class SampleNPOI_xlsx
{
static void Main(string[] args)
{
try
{
// 実行ファイルのあるフォルダパス取得
var folderPath = Path.GetDirectoryName(Assembly.GetEntryAssembly()?.Location);
if (folderPath == null) { return; }
// Excelファイルパスを作成
var filePath = Path.Combine(folderPath, "sample.xlsx");
// メモリにすべて取り込む。これなら遅延でStreamにアクセスするような問題が発生しない
byte[] fileBytes = File.ReadAllBytes(filePath);
using (var readStream = new MemoryStream(fileBytes))
using (var workbook = new XSSFWorkbook(readStream))
{
// シートを追加
workbook.CreateSheet("sample");
// あらためて書き込み用としてStreamを開く
using (var writeStream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None))
{
// 書き込み
workbook.Write(writeStream);
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
□xls/xlsx対応版
Program.cs
using System;
using System.IO;
using System.Reflection;
namespace SampleCode; // C#10~
internal class SampleNPOI
{
static void Main(string[] args)
{
try
{
// 実行ファイルのあるフォルダパス取得
var folderPath = Path.GetDirectoryName(Assembly.GetEntryAssembly()?.Location);
if (folderPath == null) { return; }
// Excelファイルパスを作成
var filePath = Path.Combine(folderPath, "sample.xlsx");
var extension = Path.GetExtension(filePath);
// メモリにすべて取り込む。これなら遅延でStreamにアクセスするような問題が発生しない
byte[] fileBytes = File.ReadAllBytes(filePath);
using (var readStream = new MemoryStream(fileBytes))
using (var workbook = WorkbookFactory.Create(readStream, extension))
{
// シートを追加
workbook.CreateSheet("sample");
// あらためて書き込み用としてStreamを開く
using (var writeStream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None))
{
// 書き込み
workbook.Write(writeStream);
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
WorkbookFactory.cs
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System;
using System.IO;
namespace SampleCode
{
public class WorkbookFactory
{
public static IWorkbook Create(Stream stream, string extension)
{
extension = extension.ToLower();
return extension switch
{
".xls" => new HSSFWorkbook(stream),
".xlsx" => new XSSFWorkbook(stream),
_ => throw new NotSupportedException("対象外の拡張子です")
};
}
public static IWorkbook Create(string extension)
{
extension = extension.ToLower();
return extension switch
{
".xls" => new HSSFWorkbook(),
".xlsx" => new XSSFWorkbook(),
_ => throw new NotSupportedException("対象外の拡張子です")
};
}
}
}