Skip to content

Commit 71ca30d

Browse files
authored
Merge pull request #234 from SyncfusionExamples/PageSetupOptions
Excel Page setup sample is added
2 parents 649cc5b + 6807a81 commit 71ca30d

File tree

3 files changed

+201
-0
lines changed

3 files changed

+201
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.14.36109.1 d17.14
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ExcelPageSetup", "ExcelPageSetup\ExcelPageSetup.csproj", "{F78E77CF-70F2-4690-97F3-BC30ED25F0CC}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{F78E77CF-70F2-4690-97F3-BC30ED25F0CC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{F78E77CF-70F2-4690-97F3-BC30ED25F0CC}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{F78E77CF-70F2-4690-97F3-BC30ED25F0CC}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{F78E77CF-70F2-4690-97F3-BC30ED25F0CC}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {176443BB-2398-47DF-85D3-609173A913E1}
24+
EndGlobalSection
25+
EndGlobal
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="Syncfusion.XlsIO.Net.Core" Version="*" />
12+
</ItemGroup>
13+
14+
</Project>
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
using Syncfusion.XlsIO;
2+
3+
class Program
4+
{
5+
static void Main()
6+
{
7+
using (ExcelEngine engine = new ExcelEngine())
8+
{
9+
IApplication app = engine.Excel;
10+
app.DefaultVersion = ExcelVersion.Xlsx;
11+
12+
IWorkbook workbook = app.Workbooks.Create(1);
13+
IWorksheet worksheet = workbook.Worksheets[0];
14+
worksheet.Name = "Monthly Sales";
15+
16+
// Title
17+
worksheet.Range["A1"].Text = "Monthly Sales Report";
18+
worksheet.Range["A1"].CellStyle.Font.Bold = true;
19+
worksheet.Range["A1"].CellStyle.Font.Size = 16;
20+
21+
// Headers
22+
worksheet.Range["A3"].Text = "Order ID";
23+
worksheet.Range["B3"].Text = "Date";
24+
worksheet.Range["C3"].Text = "Region";
25+
worksheet.Range["D3"].Text = "Salesperson";
26+
worksheet.Range["E3"].Text = "Units";
27+
worksheet.Range["F3"].Text = "Amount";
28+
worksheet.Range["A3:F3"].CellStyle.Font.Bold = true;
29+
worksheet.Range["A3:F3"].CellStyle.Color = Syncfusion.Drawing.Color.FromArgb(240, 240, 240);
30+
31+
// Generate Sales Data
32+
int row = 4;
33+
string[] regions = new[] { "North", "South", "West", "East" };
34+
Random rnd = new Random(17);
35+
DateTime month = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
36+
37+
foreach (string region in regions)
38+
{
39+
worksheet.Range["A" + row].Text = region + " Region";
40+
worksheet.Range["A" + row].CellStyle.Font.Bold = true;
41+
worksheet.Range["A" + row].CellStyle.Font.Size = 12;
42+
row++;
43+
44+
for (int count = 0; count < 20; count++)
45+
{
46+
worksheet.Range["A" + row].Text = $"ORD-{region.Substring(0, 1)}-{1000 + count}";
47+
worksheet.Range["B" + row].DateTime = month.AddDays(rnd.Next(0, 28));
48+
worksheet.Range["B" + row].NumberFormat = "dd-MMM";
49+
worksheet.Range["C" + row].Text = region;
50+
worksheet.Range["D" + row].Text = "Rep " + rnd.Next(1, 6);
51+
worksheet.Range["E" + row].Number = rnd.Next(1, 25);
52+
worksheet.Range["F" + row].Number = Math.Round(2500 + rnd.NextDouble() * 20000, 2);
53+
worksheet.Range["F" + row].NumberFormat = "#,##0.00";
54+
row++;
55+
}
56+
row++;
57+
}
58+
59+
worksheet.UsedRange.AutofitColumns();
60+
61+
// Page Setup Options
62+
IPageSetup pageSetup = worksheet.PageSetup;
63+
64+
// Set Paper Size to A4
65+
pageSetup.PaperSize = ExcelPaperSize.PaperA4;
66+
67+
// Set Orientation to Landscape
68+
pageSetup.Orientation = ExcelPageOrientation.Landscape;
69+
70+
// Set Margins in inches
71+
72+
// Top, Bottom - 0.5 inch
73+
pageSetup.TopMargin = 0.75;
74+
pageSetup.BottomMargin = 0.75;
75+
76+
// Left, Right - 0.25 inch
77+
pageSetup.LeftMargin = 0.25;
78+
pageSetup.RightMargin = 0.25;
79+
80+
// Header, Footer - 0.3 inch
81+
pageSetup.HeaderMargin = 0.3;
82+
pageSetup.FooterMargin = 0.3;
83+
84+
// Set fit to page as false.
85+
pageSetup.IsFitToPage = false;
86+
87+
// Set repeat rows as 3rd row
88+
pageSetup.PrintTitleRows = "$3:$3";
89+
90+
// Define the print area from A3 to last used row in column F
91+
int lastRow = worksheet.UsedRange.LastRow;
92+
pageSetup.PrintArea = $"A3:F{lastRow}";
93+
94+
// Apply left header as "Monthly Sales" with Calibri font of size 14 and bold
95+
pageSetup.LeftHeader = "&\"Calibri,Bold\"&14 Monthly Sales";
96+
97+
// Apply center header as "Month Year" with bold
98+
pageSetup.CenterHeader = "&B&10" + month.ToString("MMMM yyyy");
99+
100+
// Apply right header with page number and total pages
101+
pageSetup.RightHeader = "Page &P of &N";
102+
103+
// Apply left footer with sheet name
104+
pageSetup.LeftFooter = "Sheet: &A";
105+
106+
// Apply center footer with current date and time
107+
pageSetup.CenterFooter = "Generated: &D &T";
108+
109+
// Apply right footer with file name
110+
pageSetup.RightFooter = "&F";
111+
112+
// Set the scaling to 120%
113+
pageSetup.Zoom = 120;
114+
115+
// Fit to 1 page wide and unlimited tall
116+
pageSetup.FitToPagesTall = 0;
117+
118+
// Fit to 1 page wide
119+
pageSetup.FitToPagesWide = 1;
120+
121+
// Add a page break before each region. Assuming each region block (title + data) takes 22 rows including spacing.
122+
int firstRegionRow = 4;
123+
int blockHeight = 22;
124+
for (int count = 1; count < regions.Length; count++)
125+
{
126+
int titleRow = firstRegionRow + count * blockHeight;
127+
128+
// Add a horizontal page break before the title row of each region
129+
worksheet.HPageBreaks.Add(worksheet.Range["A" + titleRow]);
130+
}
131+
132+
// Set gridlines to be hidden in the printed page
133+
pageSetup.PrintGridlines = false;
134+
135+
// Set center the sheet horizontally on the page
136+
pageSetup.CenterHorizontally = true;
137+
138+
// Set not to center the sheet vertically on the page
139+
pageSetup.CenterVertically = false;
140+
141+
142+
//Do not print headings
143+
pageSetup.PrintHeadings = false;
144+
145+
// Do not print comments
146+
pageSetup.PrintComments = ExcelPrintLocation.PrintNoComments;
147+
148+
// Do print in black and white only
149+
pageSetup.BlackAndWhite = true;
150+
151+
// Draft quality set to false
152+
pageSetup.Draft = false;
153+
154+
// Set first page number as 2
155+
pageSetup.FirstPageNumber = 2;
156+
157+
// Save workbook as Excel document
158+
workbook.SaveAs("MonthlySales.xlsx");
159+
160+
}
161+
}
162+
}

0 commit comments

Comments
 (0)