From da97026edbcf4894bf30dc806cbaeac52e243ca9 Mon Sep 17 00:00:00 2001 From: shixia Date: Mon, 24 Aug 2026 08:49:33 +0800 Subject: [PATCH 1/2] Add new function: UnmergeCell --- Excelize.Tests/UnitTest.cs | 23 ++++++++++++++++++++++ Excelize/Excelize.cs | 39 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) diff --git a/Excelize.Tests/UnitTest.cs b/Excelize.Tests/UnitTest.cs index c22f5c0..99b112c 100644 --- a/Excelize.Tests/UnitTest.cs +++ b/Excelize.Tests/UnitTest.cs @@ -93,7 +93,22 @@ public void TestAddChart() Assert.Null(Record.Exception(() => f.SaveAs("TestAddChart.xlsx"))); Assert.Empty(f.Close()); } + [Fact] + public void TestMergeCell() + { + // 创建一个新的 Excel 文件 + File f = Excelize.NewFile(); + + // 合并 Sheet1 的 D3 到 E9 + Assert.Null( + Record.Exception(() => + f.MergeCell("Sheet1", "D3", "E9") + ) + ); + // 关闭文件 + Assert.Empty(f.Close()); + } [Fact] public void TestAddComment() { @@ -977,6 +992,14 @@ public void TestMergeCells() ); RuntimeError err = Assert.Throws(() => f.MergeCell("SheetN", "A1", "B2")); Assert.Equal("sheet SheetN does not exist", err.Message); + Assert.Null( + Record.Exception(() => + { + f.UnmergeCell("Sheet1", "D3", "E9"); + }) + ); + err = Assert.Throws(() => f.UnmergeCell("SheetN", "A1", "B2")); + Assert.Equal("sheet SheetN does not exist", err.Message); Assert.Null(Record.Exception(() => f.SaveAs("TestMergeCells.xlsx"))); Assert.Empty(f.Close()); } diff --git a/Excelize/Excelize.cs b/Excelize/Excelize.cs index b907571..46c7208 100644 --- a/Excelize/Excelize.cs +++ b/Excelize/Excelize.cs @@ -421,6 +421,14 @@ internal static extern IntPtr MergeCell( [MarshalAs(UnmanagedType.LPUTF8Str)] string bottomRightCell ); + [DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)] + internal static extern IntPtr UnmergeCell( + long fileIdx, + [MarshalAs(UnmanagedType.LPUTF8Str)] string sheet, + [MarshalAs(UnmanagedType.LPUTF8Str)] string topLeftCell, + [MarshalAs(UnmanagedType.LPUTF8Str)] string bottomRightCell + ); + [DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr MoveSheet( long fileIdx, @@ -6338,6 +6346,37 @@ public void UngroupSheets() throw new RuntimeError(err); } + /// + /// UnmergeCell provides a function to unmerge a given range reference. + /// + /// For example unmerge range reference D3:E9 on Sheet1: + /// + /// try + /// { + /// f.UnmergeCell("Sheet1", "D3", "E9"); + /// } + /// catch (RuntimeError err) + /// { + /// Console.WriteLine(err.Message); + /// } + /// + /// + /// + /// The worksheet name + /// The top-left cell reference + /// The bottom-right cell reference + /// Attention: overlapped range will also be unmerged + /// Return None if no error occurred, + /// otherwise raise a RuntimeError with the message. + public void UnmergeCell(string sheet, string topLeftCell, string bottomRightCell) + { + string err = Marshal.PtrToStringUTF8( + Lib.UnmergeCell(FileIdx, sheet, topLeftCell, bottomRightCell) + ); + if (!string.IsNullOrEmpty(err)) + throw new RuntimeError(err); + } + /// /// UnsetConditionalFormat provides a function to unset the conditional /// format by given worksheet name and range reference. From 52836a439856ee1a6c7d4e4b0e4a6bdc2ffb04ec Mon Sep 17 00:00:00 2001 From: shixia Date: Mon, 24 Aug 2026 08:54:05 +0800 Subject: [PATCH 2/2] Remove unuse testcase --- Excelize.Tests/UnitTest.cs | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/Excelize.Tests/UnitTest.cs b/Excelize.Tests/UnitTest.cs index 99b112c..c2371bc 100644 --- a/Excelize.Tests/UnitTest.cs +++ b/Excelize.Tests/UnitTest.cs @@ -93,22 +93,7 @@ public void TestAddChart() Assert.Null(Record.Exception(() => f.SaveAs("TestAddChart.xlsx"))); Assert.Empty(f.Close()); } - [Fact] - public void TestMergeCell() - { - // 创建一个新的 Excel 文件 - File f = Excelize.NewFile(); - - // 合并 Sheet1 的 D3 到 E9 - Assert.Null( - Record.Exception(() => - f.MergeCell("Sheet1", "D3", "E9") - ) - ); - // 关闭文件 - Assert.Empty(f.Close()); - } [Fact] public void TestAddComment() {