Skip to content

Commit 93e72f4

Browse files
committed
Add new function: SetHeaderFooter
- Update unit test - Update GitHub Action configuration for test on .Net 10.x
1 parent 5af82cd commit 93e72f4

File tree

7 files changed

+128
-2
lines changed

7 files changed

+128
-2
lines changed

.github/workflows/build.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ jobs:
4141
7.x
4242
8.x
4343
9.x
44+
10.x
4445
4546
- name: Test on Windows
4647
env:
@@ -217,6 +218,7 @@ jobs:
217218
7.x
218219
8.x
219220
9.x
221+
10.x
220222
221223
- name: Download Artifacts
222224
uses: actions/download-artifact@v5

Excelize.Tests/Excelize.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<IsPackable>false</IsPackable>
66
<IsTestProject>true</IsTestProject>
77
<Nullable>enable</Nullable>
8-
<TargetFrameworks>net6.0;net7.0;net8.0;net9.0</TargetFrameworks>
8+
<TargetFrameworks>net6.0;net7.0;net8.0;net9.0;net10.0</TargetFrameworks>
99
</PropertyGroup>
1010
<ItemGroup>
1111
<PackageReference Include="coverlet.collector" Version="6.0.4">

Excelize.Tests/UnitTest.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,41 @@ public void TestCoordinates()
529529
);
530530
}
531531

532+
[Fact]
533+
public void TestHeaderFooter()
534+
{
535+
File f = Excelize.NewFile();
536+
Assert.Null(
537+
Record.Exception(() =>
538+
{
539+
f.SetHeaderFooter(
540+
"Sheet1",
541+
new HeaderFooterOptions
542+
{
543+
DifferentFirst = true,
544+
DifferentOddEven = true,
545+
OddHeader = "&R&P",
546+
OddFooter = "&C&F",
547+
EvenHeader = "&L&P",
548+
EvenFooter = "&L&D&R&T",
549+
FirstHeader = "&CCenter &\"-,Bold\"Bold&\"-,Regular\"HeaderU+000A&D",
550+
}
551+
);
552+
})
553+
);
554+
var err = Assert.Throws<RuntimeError>(() =>
555+
f.SetHeaderFooter("SheetN", new HeaderFooterOptions { })
556+
);
557+
Assert.Equal("sheet SheetN does not exist", err.Message);
558+
Assert.Null(
559+
Record.Exception(() =>
560+
{
561+
f.SaveAs("TestHeaderFooter.xlsx");
562+
})
563+
);
564+
Assert.Empty(f.Close());
565+
}
566+
532567
[Fact]
533568
public void TestOpenFile()
534569
{

Excelize/Excelize.cs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,17 @@ internal static extern IntPtr SetSheetProps(
363363
ref TypesC.SheetPropsOptions options
364364
);
365365

366+
[DllImport(
367+
LibraryName,
368+
CallingConvention = CallingConvention.Cdecl,
369+
CharSet = CharSet.Ansi
370+
)]
371+
internal static extern IntPtr SetHeaderFooter(
372+
long fileIdx,
373+
string sheet,
374+
ref TypesC.HeaderFooterOptions opts
375+
);
376+
366377
[DllImport(
367378
LibraryName,
368379
CallingConvention = CallingConvention.Cdecl,
@@ -2145,6 +2156,48 @@ public void SetSheetProps(string sheet, SheetPropsOptions options)
21452156
throw new RuntimeError(err);
21462157
}
21472158

2159+
/// <summary>
2160+
/// Set headers and footers by given worksheet name and the control
2161+
/// characters.
2162+
/// <example>
2163+
/// For example:
2164+
/// <code><![CDATA[
2165+
/// try
2166+
/// {
2167+
/// f.SetHeaderFooter(
2168+
/// "Sheet1",
2169+
/// new HeaderFooterOptions
2170+
/// {
2171+
/// DifferentFirst = true,
2172+
/// DifferentOddEven = true,
2173+
/// OddHeader = "&R&P",
2174+
/// OddFooter = "&C&F",
2175+
/// EvenHeader = "&L&P",
2176+
/// EvenFooter = "&L&D&R&T",
2177+
/// FirstHeader = "&CCenter &\"-,Bold\"Bold&\"-,Regular\"HeaderU+000A&D",
2178+
/// }
2179+
/// );
2180+
/// }
2181+
/// catch (RuntimeError err)
2182+
/// {
2183+
/// Console.WriteLine(err.Message);
2184+
/// }]]>
2185+
/// </code>
2186+
/// </example>
2187+
/// </summary>
2188+
/// <param name="sheet">The worksheet name</param>
2189+
/// <param name="options">The header footer options</param>
2190+
/// <exception cref="RuntimeError">Return None if no error occurred,
2191+
/// otherwise raise a RuntimeError with the message.</exception>
2192+
public void SetHeaderFooter(string sheet, HeaderFooterOptions? options)
2193+
{
2194+
var opts = (TypesC.HeaderFooterOptions)
2195+
Lib.CsToC(options ?? new HeaderFooterOptions(), new TypesC.HeaderFooterOptions());
2196+
string err = Marshal.PtrToStringAnsi(Lib.SetHeaderFooter(FileIdx, sheet, ref opts));
2197+
if (!string.IsNullOrEmpty(err))
2198+
throw new RuntimeError(err);
2199+
}
2200+
21482201
/// <summary>
21492202
/// Writes cells to row by given worksheet name, starting cell reference
21502203
/// and cell values list.

Excelize/Excelize.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
<RepositoryBranch>main</RepositoryBranch>
2525
<RepositoryType>git</RepositoryType>
2626
<RepositoryUrl>https://github.com/xuri/excelize-cs.git</RepositoryUrl>
27-
<TargetFrameworks>net6.0;net7.0;net8.0;net9.0</TargetFrameworks>
27+
<TargetFrameworks>net6.0;net7.0;net8.0;net9.0;net10.0</TargetFrameworks>
2828
<Title>Excelize</Title>
2929
<Version>0.0.4</Version>
3030
</PropertyGroup>

Excelize/TypesC.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,25 @@ public struct Protection
118118
public bool Locked;
119119
}
120120

121+
[StructLayout(LayoutKind.Sequential)]
122+
public unsafe struct HeaderFooterOptions
123+
{
124+
public bool* AlignWithMargins;
125+
126+
[MarshalAs(UnmanagedType.I1)]
127+
public bool DifferentFirst;
128+
129+
[MarshalAs(UnmanagedType.I1)]
130+
public bool DifferentOddEven;
131+
public bool* ScaleWithDoc;
132+
public sbyte* OddHeader;
133+
public sbyte* OddFooter;
134+
public sbyte* EvenHeader;
135+
public sbyte* EvenFooter;
136+
public sbyte* FirstHeader;
137+
public sbyte* FirstFooter;
138+
}
139+
121140
[StructLayout(LayoutKind.Sequential)]
122141
public unsafe struct Style
123142
{

Excelize/TypesCs.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,23 @@ public struct Protection
291291
public bool Hidden;
292292
}
293293

294+
/// <summary>
295+
/// HeaderFooterOptions directly maps the settings of header and footer.
296+
/// </summary>
297+
public struct HeaderFooterOptions
298+
{
299+
public bool? AlignWithMargins;
300+
public bool DifferentFirst;
301+
public bool DifferentOddEven;
302+
public bool? ScaleWithDoc;
303+
public string OddHeader;
304+
public string OddFooter;
305+
public string EvenHeader;
306+
public string EvenFooter;
307+
public string FirstHeader;
308+
public string FirstFooter;
309+
}
310+
294311
/// <summary>
295312
/// Style directly maps the style settings of the cells.
296313
/// </summary>

0 commit comments

Comments
 (0)