Skip to content

Commit 65a1515

Browse files
committed
Excel Page setup sample is added
1 parent 649cc5b commit 65a1515

File tree

3 files changed

+205
-0
lines changed

3 files changed

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

0 commit comments

Comments
 (0)