Page Setup Options in Excel Document
14 Oct 202424 minutes to read
Fit all rows on one page
FitToPagesTall enables the functionality of fitting all rows on 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 Using
A 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 enables the functionality of fitting all columns on 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 Using
A complete working example to fit all columns on one page in C# is present on this GitHub page.
Fit the page content.
IsFitToPage enables the functionality of fitting 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 Using
A 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 Using
A 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 Using
A complete working example to enable IsSummaryRowBelow in C# is present on this GitHub page.
Print Area.
The PrintArea functionality allows you to set 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 Using
A complete working example to set the range to be printed in C# is present on this GitHub page.
Print Gridlines.
The PrintGridlines functionality allows you to set the gridlines to be printed.
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 Using
A complete working example to set the gridlines to be printed in C# is present on this GitHub page.
Print Headings.
The PrintHeadings functionality allows you to set the row and column headings to be printed.
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 Using
A 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 functionality allows you to specify the columns containing cells that will be repeated 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 Using
A complete working example to set the PrintTitleColumns in C# is present on this GitHub page.
Print TitleRows.
The PrintTitleRows functionality allows you to specify the rows containing cells that will be repeated on the top side 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 Using
A 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 formatting to page headers
worksheet.PageSetup.LeftHeader = "&KFF0000 Left Header";
worksheet.PageSetup.CenterHeader = "&KFF0000 Center Header";
worksheet.PageSetup.RightHeader = "&KFF0000 Right Header";
//Adding text with formatting and image to page footers
worksheet.PageSetup.LeftFooter = "&B &18 &K0000FF Left Footer";
FileStream imageStream = new FileStream(Path.GetFullPath(@"Data/image.jpg"), FileMode.Open);
worksheet.PageSetup.CenterFooter = "&G";
worksheet.PageSetup.CenterFooterImage = Image.FromStream(imageStream);
worksheet.PageSetup.RightFooter = "&P &K0000FF Right Footer";
//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 values in worksheet
worksheet.Range["A1:A600"].Text = "HelloWorld";
//Adding text with formatting to page headers
worksheet.PageSetup.LeftHeader = "&KFF0000 Left Header";
worksheet.PageSetup.CenterHeader = "&KFF0000 Center Header";
worksheet.PageSetup.RightHeader = "&KFF0000 Right Header";
//Adding text with formatting and image to page footers
worksheet.PageSetup.LeftFooter = "&B &18 &K0000FF Left Footer";
worksheet.PageSetup.CenterFooter = "&G";
worksheet.PageSetup.CenterFooterImage = Image.FromFile("Image.jpg");
worksheet.PageSetup.RightFooter = "&P &K0000FF Right Footer";
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 formatting to page headers
worksheet.PageSetup.LeftHeader = "&KFF0000 Left Header"
worksheet.PageSetup.CenterHeader = "&KFF0000 Center Header"
worksheet.PageSetup.RightHeader = "&KFF0000 Right Header"
'Adding text with formatting and image to page footers
worksheet.PageSetup.LeftFooter = "&B &18 &K0000FF Left Footer"
worksheet.PageSetup.CenterFooter = "&G"
worksheet.PageSetup.CenterFooterImage = Image.FromFile("Image.jpg")
worksheet.PageSetup.RightFooter = "&P &K0000FF Right Footer"
workbook.SaveAs("Output.xlsx")
End Using
A complete working example to add headers and footers in an Excel document using C# is present on this GitHub page.