Page Setup Options in Excel Document
24 Jul 202624 minutes to read
This article explains how to configure page setup options — page fit, print area, print titles, headers/footers, paper size, and orientation — in an Excel worksheet using Syncfusion® XlsIO.
Fit all rows on one page
FitToPagesTall fits all rows onto one printed page.
The following code example illustrates how to use FitToPagesTall.
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
IWorkbook workbook = application.Workbooks.Create(1);
IWorksheet sheet = workbook.Worksheets[0];
for (int i = 1; i <= 50; i++)
{
for (int j = 1; j <= 50; j++)
{
sheet.Range[i, j].Text = sheet.Range[i, j].AddressLocal;
}
}
#region PageSetup Settings
//Sets the fit to page tall as true.
sheet.PageSetup.FitToPagesTall = 1;
sheet.PageSetup.FitToPagesWide = 0;
#endregion
#region Save
//Saving the workbook
FileStream outputStream = new FileStream(Path.GetFullPath("Output/FitToPagesTall.xlsx"), FileMode.Create, FileAccess.Write);
workbook.SaveAs(outputStream);
#endregion
//Dispose streams
outputStream.Dispose();
}using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
IWorkbook workbook = application.Workbooks.Create(1);
IWorksheet sheet = workbook.Worksheets[0];
for (int i = 1; i <= 50; i++)
{
for (int j = 1; j <= 50; j++)
{
sheet.Range[i, j].Text = sheet.Range[i, j].AddressLocal;
}
}
//Sets the fit to page tall
sheet.PageSetup.FitToPagesTall = 1;
sheet.PageSetup.FitToPagesWide = 0;
//Saving the workbook
workbook.SaveAs("Output.xlsx");
}Using excelEngine As New ExcelEngine()
Dim application As IApplication = excelEngine.Excel
application.DefaultVersion = ExcelVersion.Xlsx
Dim workbook As IWorkbook = application.Workbooks.Create(1)
Dim sheet As IWorksheet = workbook.Worksheets(0)
For i As Integer = 1 To 50
For j As Integer = 1 To 50
sheet.Range(i, j).Text = sheet.Range(i, j).AddressLocal
Next
Next
' Sets the fit to page tall
sheet.PageSetup.FitToPagesTall = 1
sheet.PageSetup.FitToPagesWide = 0
'Saving the workbook
workbook.SaveAs("Output.xlsx")
End UsingA complete working example to fit all rows on one page in C# is present on this GitHub page.
Fit all columns on one page
FitToPagesWide fits all columns onto one printed page.
The following code example illustrates how to use FitToPagesWide.
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
IWorkbook workbook = application.Workbooks.Create(1);
IWorksheet sheet = workbook.Worksheets[0];
for (int i = 1; i <= 50; i++)
{
for (int j = 1; j <= 50; j++)
{
sheet.Range[i, j].Text = sheet.Range[i, j].AddressLocal;
}
}
#region PageSetup Settings
//Sets the fit to page wide as true.
sheet.PageSetup.FitToPagesWide = 1;
sheet.PageSetup.FitToPagesTall = 0;
#endregion
#region Save
//Saving the workbook
FileStream outputStream = new FileStream(Path.GetFullPath("Output/FitToPagesWide.xlsx"), FileMode.Create, FileAccess.Write);
workbook.SaveAs(outputStream);
#endregion
//Dispose streams
outputStream.Dispose();
}using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
IWorkbook workbook = application.Workbooks.Create(1);
IWorksheet sheet = workbook.Worksheets[0];
for (int i = 1; i <= 50; i++)
{
for (int j = 1; j <= 50; j++)
{
sheet.Range[i, j].Text = sheet.Range[i, j].AddressLocal;
}
}
//Sets the fit to page wide
sheet.PageSetup.FitToPagesWide = 1;
sheet.PageSetup.FitToPagesTall = 0;
//Saving the workbook
workbook.SaveAs("Output.xlsx");
}Using excelEngine As New ExcelEngine()
Dim application As IApplication = excelEngine.Excel
application.DefaultVersion = ExcelVersion.Xlsx
Dim workbook As IWorkbook = application.Workbooks.Create(1)
Dim sheet As IWorksheet = workbook.Worksheets(0)
For i As Integer = 1 To 50
For j As Integer = 1 To 50
sheet.Range(i, j).Text = sheet.Range(i, j).AddressLocal
Next
Next
' Sets the fit to page wide
sheet.PageSetup.FitToPagesWide = 1
sheet.PageSetup.FitToPagesTall = 0
'Saving the workbook
workbook.SaveAs("Output.xlsx")
End UsingA complete working example to fit all columns on one page in C# is present on this GitHub page.
Fit the page content
IsFitToPage fits the page content before printing.
The following code example illustrates how to use IsFitToPage.
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Excel2013;
IWorkbook workbook = application.Workbooks.Create(1);
IWorksheet sheet = workbook.Worksheets[0];
for (int i = 1; i <= 50; i++)
{
for (int j = 1; j <= 50; j++)
{
sheet.Range[i, j].Text = sheet.Range[i, j].AddressLocal;
}
}
#region PageSetup Settings
// True to fit the content before printing
sheet.PageSetup.IsFitToPage = true;
#endregion
#region Save
//Saving the workbook
FileStream outputStream = new FileStream(Path.GetFullPath("Output/IsFitToPage.xlsx"), FileMode.Create, FileAccess.Write);
workbook.SaveAs(outputStream);
#endregion
//Dispose streams
outputStream.Dispose();
}using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
IWorkbook workbook = application.Workbooks.Create(1);
IWorksheet sheet = workbook.Worksheets[0];
for (int i = 1; i <= 50; i++)
{
for (int j = 1; j <= 50; j++)
{
sheet.Range[i, j].Text = sheet.Range[i, j].AddressLocal;
}
}
// True to fit the content before printing
sheet.PageSetup.IsFitToPage = true;
//Saving the workbook
workbook.SaveAs("Output.xlsx");
}Using excelEngine As New ExcelEngine()
Dim application As IApplication = excelEngine.Excel
application.DefaultVersion = ExcelVersion.Xlsx
Dim workbook As IWorkbook = application.Workbooks.Create(1)
Dim sheet As IWorksheet = workbook.Worksheets(0)
For i As Integer = 1 To 50
For j As Integer = 1 To 50
sheet.Range(i, j).Text = sheet.Range(i, j).AddressLocal
Next
Next
'True to fit the content before printing
sheet.PageSetup.IsFitToPage = true
'Saving the workbook
workbook.SaveAs("Output.xlsx")
End UsingA complete working example to fit page content before printing in C# is present on this GitHub page.
Summary Column Right
To enable the IsSummaryColumnRight property, the page orientation must be Portrait, the FitToPagesTall property value must be 0 and the IsFitToPage property must be true.
The following code snippet shows how to use IsSummaryColumnRight.
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
IWorkbook workbook = application.Workbooks.Create(1);
IWorksheet sheet = workbook.Worksheets[0];
for (int i = 1; i <= 50; i++)
{
for (int j = 1; j <= 50; j++)
{
sheet.Range[i, j].Text = sheet.Range[i, j].AddressLocal;
}
}
#region PageSetup Settings
//True to summary columns will appear right of the detail in outlines
sheet.PageSetup.IsSummaryColumnRight = true;
sheet.PageSetup.Orientation = ExcelPageOrientation.Portrait;
sheet.PageSetup.FitToPagesTall = 0;
sheet.PageSetup.IsFitToPage = true;
#endregion
#region Save
//Saving the workbook
FileStream outputStream = new FileStream(Path.GetFullPath("Output/SummaryColumnRight.xlsx"), FileMode.Create, FileAccess.Write);
workbook.SaveAs(outputStream);
#endregion
//Dispose streams
outputStream.Dispose();
}using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
IWorkbook workbook = application.Workbooks.Create(1);
IWorksheet sheet = workbook.Worksheets[0];
for (int i = 1; i <= 50; i++)
{
for (int j = 1; j <= 50; j++)
{
sheet.Range[i, j].Text = sheet.Range[i, j].AddressLocal;
}
}
//True to summary columns will appear right of the detail in outlines
sheet.PageSetup.IsSummaryColumnRight = true;
sheet.PageSetup.Orientation = ExcelPageOrientation.Portrait;
sheet.PageSetup.FitToPagesTall = 0;
sheet.PageSetup.IsFitToPage = true;
//Saving the workbook
workbook.SaveAs("Output.xlsx");
}Using excelEngine As New ExcelEngine()
Dim application As IApplication = excelEngine.Excel
application.DefaultVersion = ExcelVersion.Xlsx
Dim workbook As IWorkbook = application.Workbooks.Create(1)
Dim sheet As IWorksheet = workbook.Worksheets(0)
For i As Integer = 1 To 50
For j As Integer = 1 To 50
sheet.Range(i, j).Text = sheet.Range(i, j).AddressLocal
Next
Next
'True to summary columns will appear right of the detail in outlines
sheet.PageSetup.IsSummaryColumnRight = true
sheet.PageSetup.Orientation = ExcelPageOrientation.Portrait
sheet.PageSetup.FitToPagesTall = 0
sheet.PageSetup.IsFitToPage = true
'Saving the workbook
workbook.SaveAs("Output.xlsx")
End UsingA complete working example to enable IsSummaryColumnRight in C# is present on this GitHub page.
Summary Row Below.
To enable the IsSummaryRowBelow property, the page orientation must be Portrait, the FitToPagesWide property value must be 0 and the IsFitToPage property must be true.
The following code snippet shows how to use IsSummaryRowBelow.
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
IWorkbook workbook = application.Workbooks.Create(1);
IWorksheet sheet = workbook.Worksheets[0];
for (int i = 1; i <= 50; i++)
{
for (int j = 1; j <= 50; j++)
{
sheet.Range[i, j].Text = sheet.Range[i, j].AddressLocal;
}
}
#region PageSetup Settings
//True to summary rows will appear below detail in outlines
sheet.PageSetup.IsSummaryRowBelow = true;
sheet.PageSetup.FitToPagesWide = 0;
sheet.PageSetup.Orientation = ExcelPageOrientation.Portrait;
sheet.PageSetup.IsFitToPage = true;
#endregion
#region Save
//Saving the workbook
FileStream outputStream = new FileStream(Path.GetFullPath("Output/SummaryRowBelow.xlsx"), FileMode.Create, FileAccess.Write);
workbook.SaveAs(outputStream);
#endregion
//Dispose streams
outputStream.Dispose();
}using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
IWorkbook workbook = application.Workbooks.Create(1);
IWorksheet sheet = workbook.Worksheets[0];
for (int i = 1; i <= 50; i++)
{
for (int j = 1; j <= 50; j++)
{
sheet.Range[i, j].Text = sheet.Range[i, j].AddressLocal;
}
}
//True to summary rows will appear below detail in outlines
sheet.PageSetup.IsSummaryRowBelow = true;
sheet.PageSetup.FitToPagesWide = 0;
sheet.PageSetup.Orientation = ExcelPageOrientation.Portrait;
sheet.PageSetup.IsFitToPage = true;
//Saving the workbook
workbook.SaveAs("Output.xlsx");
}Using excelEngine As New ExcelEngine()
Dim application As IApplication = excelEngine.Excel
application.DefaultVersion = ExcelVersion.Xlsx
Dim workbook As IWorkbook = application.Workbooks.Create(1)
Dim sheet As IWorksheet = workbook.Worksheets(0)
For i As Integer = 1 To 50
For j As Integer = 1 To 50
sheet.Range(i, j).Text = sheet.Range(i, j).AddressLocal
Next
Next
'True to summary rows will appear below detail in outlines
sheet.PageSetup.IsSummaryRowBelow = true
sheet.PageSetup.FitToPagesWide = 0
sheet.PageSetup.Orientation = ExcelPageOrientation.Portrait
sheet.PageSetup.IsFitToPage = true
'Saving the workbook
workbook.SaveAs("Output.xlsx")
End UsingA complete working example to enable IsSummaryRowBelow in C# is present on this GitHub page.
Print Area
The PrintArea property specifies the range to be printed.
The following code snippet shows how to use the PrintArea.
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
IWorkbook workbook = application.Workbooks.Create(1);
IWorksheet sheet = workbook.Worksheets[0];
for (int i = 1; i <= 50; i++)
{
for (int j = 1; j <= 50; j++)
{
sheet.Range[i, j].Text = sheet.Range[i, j].AddressLocal;
}
}
#region PageSetup Settings
//Sets the range to be printed
sheet.PageSetup.PrintArea = "A1:M20";
#endregion
#region Save
//Saving the workbook
FileStream outputStream = new FileStream(Path.GetFullPath("Output/PrintArea.xlsx"), FileMode.Create, FileAccess.Write);
workbook.SaveAs(outputStream);
#endregion
//Dispose streams
outputStream.Dispose();
}using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
IWorkbook workbook = application.Workbooks.Create(1);
IWorksheet sheet = workbook.Worksheets[0];
for (int i = 1; i <= 50; i++)
{
for (int j = 1; j <= 50; j++)
{
sheet.Range[i, j].Text = sheet.Range[i, j].AddressLocal;
}
}
//Sets the range to be printed
sheet.PageSetup.PrintArea = "A1:M20";
//Saving the workbook
workbook.SaveAs("Output.xlsx");
}Using excelEngine As New ExcelEngine()
Dim application As IApplication = excelEngine.Excel
application.DefaultVersion = ExcelVersion.Xlsx
Dim workbook As IWorkbook = application.Workbooks.Create(1)
Dim sheet As IWorksheet = workbook.Worksheets(0)
For i As Integer = 1 To 50
For j As Integer = 1 To 50
sheet.Range(i, j).Text = sheet.Range(i, j).AddressLocal
Next
Next
'Sets the range to be printed
sheet.PageSetup.PrintArea = "A1:M20"
'Saving the workbook
workbook.SaveAs("Output.xlsx")
End UsingA complete working example to set the range to be printed in C# is present on this GitHub page.
Print Gridlines
The PrintGridlines property prints cell gridlines on each page.
The following code snippet shows how to use PrintGridlines.
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
IWorkbook workbook = application.Workbooks.Create(1);
IWorksheet sheet = workbook.Worksheets[0];
for (int i = 1; i <= 50; i++)
{
for (int j = 1; j <= 50; j++)
{
sheet.Range[i, j].Text = sheet.Range[i, j].AddressLocal;
}
}
#region PageSetup Settings
//True to cell gridlines are printed on the page
sheet.PageSetup.PrintGridlines = true;
#endregion
#region Save
//Saving the workbook
FileStream outputStream = new FileStream(Path.GetFullPath("Output/PrintGridlines.xlsx"), FileMode.Create, FileAccess.Write);
workbook.SaveAs(outputStream);
#endregion
//Dispose streams
outputStream.Dispose();
}using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
IWorkbook workbook = application.Workbooks.Create(1);
IWorksheet sheet = workbook.Worksheets[0];
for (int i = 1; i <= 50; i++)
{
for (int j = 1; j <= 50; j++)
{
sheet.Range[i, j].Text = sheet.Range[i, j].AddressLocal;
}
}
//True to cell gridlines are printed on the page
sheet.PageSetup.PrintGridlines = true;
//Saving the workbook
workbook.SaveAs("Output.xlsx");
}Using excelEngine As New ExcelEngine()
Dim application As IApplication = excelEngine.Excel
application.DefaultVersion = ExcelVersion.Xlsx
Dim workbook As IWorkbook = application.Workbooks.Create(1)
Dim sheet As IWorksheet = workbook.Worksheets(0)
For i As Integer = 1 To 50
For j As Integer = 1 To 50
sheet.Range(i, j).Text = sheet.Range(i, j).AddressLocal
Next
Next
'True to cell gridlines are printed on the page
sheet.PageSetup.PrintGridlines = true
'Saving the workbook
workbook.SaveAs("Output.xlsx")
End UsingA complete working example to set the gridlines to be printed in C# is present on this GitHub page.
Print Headings
The PrintHeadings property prints row and column headings on each page.
The following code snippet shows how to use PrintHeadings.
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
IWorkbook workbook = application.Workbooks.Create(1);
IWorksheet sheet = workbook.Worksheets[0];
for (int i = 1; i <= 50; i++)
{
for (int j = 1; j <= 50; j++)
{
sheet.Range[i, j].Text = sheet.Range[i, j].AddressLocal;
}
}
#region PageSetup Settings
//True to row and column headings are printed on page
sheet.PageSetup.PrintHeadings = true;
#endregion
#region Save
//Saving the workbook
FileStream outputStream = new FileStream(Path.GetFullPath("Output/PrintHeadings.xlsx"), FileMode.Create, FileAccess.Write);
workbook.SaveAs(outputStream);
#endregion
//Dispose streams
outputStream.Dispose();
}using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
IWorkbook workbook = application.Workbooks.Create(1);
IWorksheet sheet = workbook.Worksheets[0];
for (int i = 1; i <= 50; i++)
{
for (int j = 1; j <= 50; j++)
{
sheet.Range[i, j].Text = sheet.Range[i, j].AddressLocal;
}
}
//True to row and column headings are printed on page
sheet.PageSetup.PrintHeadings = true;
//Saving the workbook
workbook.SaveAs("Output.xlsx");
}Using excelEngine As New ExcelEngine()
Dim application As IApplication = excelEngine.Excel
application.DefaultVersion = ExcelVersion.Xlsx
Dim workbook As IWorkbook = application.Workbooks.Create(1)
Dim sheet As IWorksheet = workbook.Worksheets(0)
For i As Integer = 1 To 50
For j As Integer = 1 To 50
sheet.Range(i, j).Text = sheet.Range(i, j).AddressLocal
Next
Next
'True to row and column headings are printed on page
sheet.PageSetup.PrintHeadings = true
'Saving the workbook
workbook.SaveAs("Output.xlsx")
End UsingA complete working example to set the row and column headings to be printed in C# is present on this GitHub page.
Print TitleColumns
The PrintTitleColumns property specifies the columns to repeat on the left side of each printed page.
The following code snippet shows how to use PrintTitleColumns.
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
IWorkbook workbook = application.Workbooks.Create(1);
IWorksheet sheet = workbook.Worksheets[0];
for (int i = 1; i <= 50; i++)
{
for (int j = 1; j <= 50; j++)
{
sheet.Range[i, j].Text = sheet.Range[i, j].AddressLocal;
}
}
#region PageSetup Settings
//Sets the columns to be repeated on the left side of each page
sheet.PageSetup.PrintTitleColumns = "C1:C50";
#endregion
#region Save
//Saving the workbook
FileStream outputStream = new FileStream(Path.GetFullPath("Output/PrintTitleColumns.xlsx"), FileMode.Create, FileAccess.Write);
workbook.SaveAs(outputStream);
#endregion
//Dispose streams
outputStream.Dispose();
}using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
IWorkbook workbook = application.Workbooks.Create(1);
IWorksheet sheet = workbook.Worksheets[0];
for (int i = 1; i <= 50; i++)
{
for (int j = 1; j <= 50; j++)
{
sheet.Range[i, j].Text = sheet.Range[i, j].AddressLocal;
}
}
//Sets the columns to be repeated on the left side of each page
sheet.PageSetup.PrintTitleColumns = "C1:C50";
//Saving the workbook
workbook.SaveAs("Output.xlsx");
}Using excelEngine As New ExcelEngine()
Dim application As IApplication = excelEngine.Excel
application.DefaultVersion = ExcelVersion.Xlsx
Dim workbook As IWorkbook = application.Workbooks.Create(1)
Dim sheet As IWorksheet = workbook.Worksheets(0)
For i As Integer = 1 To 50
For j As Integer = 1 To 50
sheet.Range(i, j).Text = sheet.Range(i, j).AddressLocal
Next
Next
'Sets the columns to be repeated on the left side of each page
sheet.PageSetup.PrintTitleColumns = "C1:C50"
'Saving the workbook
workbook.SaveAs("Output.xlsx")
End UsingA complete working example to set the PrintTitleColumns in C# is present on this GitHub page.
Print TitleRows
The PrintTitleRows property specifies the rows to repeat at the top of each printed page.
The following code snippet shows how to use PrintTitleRows.
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
IWorkbook workbook = application.Workbooks.Create(1);
IWorksheet sheet = workbook.Worksheets[0];
for (int i = 1; i <= 50; i++)
{
for (int j = 1; j <= 50; j++)
{
sheet.Range[i, j].Text = sheet.Range[i, j].AddressLocal;
}
}
#region PageSetup Settings
//Sets the rows to be repeated at the top of each page
sheet.PageSetup.PrintTitleRows = "A1:AX1";
#endregion
#region Save
//Saving the workbook
FileStream outputStream = new FileStream(Path.GetFullPath("Output/PrintTitleRows.xlsx"), FileMode.Create, FileAccess.Write);
workbook.SaveAs(outputStream);
#endregion
//Dispose streams
outputStream.Dispose();
}using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
IWorkbook workbook = application.Workbooks.Create(1);
IWorksheet sheet = workbook.Worksheets[0];
for (int i = 1; i <= 50; i++)
{
for (int j = 1; j <= 50; j++)
{
sheet.Range[i, j].Text = sheet.Range[i, j].AddressLocal;
}
}
//Sets the rows to be repeated at the top of each page
sheet.PageSetup.PrintTitleRows = "A1:AX1";
//Saving the workbook
workbook.SaveAs("Output.xlsx");
}Using excelEngine As New ExcelEngine()
Dim application As IApplication = excelEngine.Excel
application.DefaultVersion = ExcelVersion.Xlsx
Dim workbook As IWorkbook = application.Workbooks.Create(1)
Dim sheet As IWorksheet = workbook.Worksheets(0)
For i As Integer = 1 To 50
For j As Integer = 1 To 50
sheet.Range(i, j).Text = sheet.Range(i, j).AddressLocal
Next
Next
'Sets the rows to be repeated at the top of each page
sheet.PageSetup.PrintTitleRows = "A1:AX1"
'Saving the workbook
workbook.SaveAs("Output.xlsx")
End UsingA complete working example to set the PrintTitleRows in C# is present on this GitHub page.
Headers and Footers
The following code example illustrates how to add headers and footers in an Excel document.
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Excel2013;
IWorkbook workbook = application.Workbooks.Create(1);
IWorksheet worksheet = workbook.Worksheets[0];
//Adding values in worksheet
worksheet.Range["A1:A600"].Text = "HelloWorld";
//Adding text with red color formatting to the left header
worksheet.PageSetup.LeftHeader = "&KFF0000 Left Header";
//Adding text (sheet name) with red color formatting to the center header
worksheet.PageSetup.CenterHeader = "&KFF0000&A";
//Adding text (date and time) with red color formatting to the right header
worksheet.PageSetup.RightHeader = "&KFF0000&D &T";
//Adding bold text, font size 18, and blue color formatting to the left footer
worksheet.PageSetup.LeftFooter = "&B &18 &K0000FF Left Footer";
//Adding an image placeholder to the center footer
FileStream imageStream = new FileStream(Path.GetFullPath(@"Data/image.jpg"), FileMode.Open);
worksheet.PageSetup.CenterFooter = "&G";
worksheet.PageSetup.CenterFooterImage = Image.FromStream(imageStream);
//Adding the file name to the right footer with blue color formatting
worksheet.PageSetup.RightFooter = "&K0000FF&F";
//Saving the workbook as stream
FileStream stream = new FileStream(Path.GetFullPath("Output/Output.xlsx"), FileMode.Create);
workbook.SaveAs(stream);
stream.Dispose();
}using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Excel2013;
IWorkbook workbook = application.Workbooks.Create(1);
IWorksheet worksheet = workbook.Worksheets[0];
//Adding text with red color formatting to the left header
worksheet.PageSetup.LeftHeader = "&KFF0000 Left Header";
//Adding text (sheet name) with red color formatting to the center header
worksheet.PageSetup.CenterHeader = "&KFF0000&A";
//Adding text (date and time) with red color formatting to the right header
worksheet.PageSetup.RightHeader = "&KFF0000&D &T";
//Adding bold text, font size 18, and blue color formatting to the left footer
worksheet.PageSetup.LeftFooter = "&B &18 &K0000FF Left Footer";
//Adding an image placeholder to the center footer. Requires: using System.Drawing;
FileStream imageStream = new FileStream(Path.GetFullPath(@"Data/image.jpg"), FileMode.Open);
worksheet.PageSetup.CenterFooter = "&G";
worksheet.PageSetup.CenterFooterImage = Image.FromStream(imageStream);
//Adding the file name to the right footer with blue color formatting
worksheet.PageSetup.RightFooter = "&K0000FF&F";
workbook.SaveAs("Output.xlsx");
}Using excelEngine As ExcelEngine = New ExcelEngine()
Dim application As IApplication = excelEngine.Excel
application.DefaultVersion = ExcelVersion.Excel2013
Dim workbook As IWorkbook = application.Workbooks.Create(1)
Dim worksheet As IWorksheet = workbook.Worksheets(0)
'Adding values in worksheet
worksheet.Range("A1:A600").Text = "HelloWorld"
'Adding text with red color formatting to the left header
worksheet.PageSetup.LeftHeader = "&KFF0000 Left Header"
'Adding text (sheet name) with red color formatting to the center header
worksheet.PageSetup.CenterHeader = "&KFF0000&A"
'Adding text (date and time) with red color formatting to the right header
worksheet.PageSetup.RightHeader = "&KFF0000&D &T"
'Adding bold text, font size 18, and blue color formatting to the left footer
worksheet.PageSetup.LeftFooter = "&B &18 &K0000FF Left Footer"
'Adding an image placeholder to the center footer. Requires: Imports System.Drawing
FileStream imageStream = new FileStream(Path.GetFullPath(@"Data/image.jpg"), FileMode.Open)
worksheet.PageSetup.CenterFooter = "&G"
worksheet.PageSetup.CenterFooterImage = Image.FromStream(imageStream)
'Adding the file name to the right footer with blue color formatting
worksheet.PageSetup.RightFooter = "&K0000FF&F"
workbook.SaveAs("Output.xlsx")
End UsingA complete working example to add headers and footers in an Excel document using C# is present on this GitHub page.
Paper Size
The PaperSize property specifies the paper size for the worksheet.
The following code snippet shows how to use PaperSize.
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
IWorkbook workbook = application.Workbooks.Open(Path.GetFullPath(@"Data/InputTemplate.xlsx"));
IWorksheet worksheet = workbook.Worksheets[0];
//Set the paper size to A4
worksheet.PageSetup.PaperSize = ExcelPaperSize.PaperA4;
//Saving the workbook
workbook.SaveAs(Path.GetFullPath("Output/Output.xlsx"));
}using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
IWorkbook workbook = application.Workbooks.Open("InputTemplate.xlsx");
IWorksheet worksheet = workbook.Worksheets[0];
//Set the paper size to A4
worksheet.PageSetup.PaperSize = ExcelPaperSize.PaperA4;
//Saving the workbook
workbook.SaveAs("Output.xlsx");
}Using excelEngine As ExcelEngine = New ExcelEngine()
Dim application As IApplication = excelEngine.Excel
application.DefaultVersion = ExcelVersion.Xlsx
Dim workbook As IWorkbook = application.Workbooks.Open("InputTemplate.xlsx")
Dim worksheet As IWorksheet = workbook.Worksheets(0)
'Set the paper size to A4
worksheet.PageSetup.PaperSize = ExcelPaperSize.PaperA4
'Saving the workbook
workbook.SaveAs("Output.xlsx")
End UsingA complete working example to set the paper size in C# is present on this GitHub page.
Orientation
The Orientation property specifies the page orientation for the worksheet.
The following code snippet shows how to use Orientation.
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
IWorkbook workbook = application.Workbooks.Open(Path.GetFullPath(@"Data/InputTemplate.xlsx"));
IWorksheet worksheet = workbook.Worksheets[0];
//Set the page orientation
worksheet.PageSetup.Orientation = ExcelPageOrientation.Landscape;
//Saving the workbook
workbook.SaveAs(Path.GetFullPath("Output/Output.xlsx"));
}using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
IWorkbook workbook = application.Workbooks.Open("InputTemplate.xlsx");
IWorksheet worksheet = workbook.Worksheets[0];
//Set the page orientation
worksheet.PageSetup.Orientation = ExcelPageOrientation.Landscape;
//Saving the workbook
workbook.SaveAs("Output.xlsx");
}Using excelEngine As ExcelEngine = New ExcelEngine()
Dim application As IApplication = excelEngine.Excel
application.DefaultVersion = ExcelVersion.Xlsx
Dim workbook As IWorkbook = application.Workbooks.Open("InputTemplate.xlsx")
Dim worksheet As IWorksheet = workbook.Worksheets(0)
'Set the page orientation
worksheet.PageSetup.Orientation = ExcelPageOrientation.Landscape
'Saving the workbook
workbook.SaveAs("Output.xlsx")
End UsingA complete working example to set the page orientation in C# is present on this GitHub page.
See also
- Explore the rich set of Syncfusion® Excel library (XlsIO) features at Syncfusion Excel Framework for .NET Core.