How to set print titles?

8 Dec 20234 minutes to read

Printing Title Rows

XlsIO allows to designate row header to repeat on all pages of a printed workbook using PrintTitleRows property. The following code snippet illustrates this.

using (ExcelEngine excelEngine = new ExcelEngine())
{
  IApplication application = excelEngine.Excel;
  FileStream inputStream = new FileStream("Sample.xlsx", FileMode.Open, FileAccess.Read);
  IWorkbook workbook = application.Workbooks.Open(inputStream, ExcelOpenType.Automatic);
  IWorksheet worksheet = workbook.Worksheets[0];

  //Print Rows 1 to 3 on every printed page
  worksheet.PageSetup.PrintTitleRows = "$A$1:$IV$3";

  FileStream stream = new FileStream("TitleRows.xlsx", FileMode.OpenOrCreate, FileAccess.ReadWrite);
  workbook.SaveAs(stream);
  workbook.Close();
  excelEngine.Dispose();
}
using (ExcelEngine excelEngine = new ExcelEngine())
{
  IApplication application = excelEngine.Excel;
  application.DefaultVersion = ExcelVersion.Excel2013;
  IWorkbook workbook = application.Workbooks.Open("Sample.xlsx", ExcelOpenType.Automatic);
  IWorksheet worksheet = workbook.Worksheets[0];

  //Print Rows 1 to 3 on every printed page
  worksheet.PageSetup.PrintTitleRows = "$A$1:$IV$3";
  workbook.SaveAs("TitleRows.xlsx");
}
Using excelEngine As ExcelEngine = New ExcelEngine()
  Dim application As IApplication = excelEngine.Excel
  application.DefaultVersion = ExcelVersion.Excel2013
  Dim workbook As IWorkbook = application.Workbooks.Open("Sample.xlsx", ExcelOpenType.Automatic)
  Dim worksheet As IWorksheet = workbook.Worksheets(0)

  'Print Rows 1 to 3 on every printed page
  worksheet.PageSetup.PrintTitleRows = "$A$1:$IV$3"
  workbook.SaveAs("TitleRows.xlsx")
End Using

Printing Title Columns

XlsIO allows to designate column header to repeat on all pages of a printed workbook using PrintTitleColumns property. The following code illustrates printing Title Columns.

using (ExcelEngine excelEngine = new ExcelEngine())
{
  IApplication application = excelEngine.Excel;
  FileStream inputStream = new FileStream("Sample.xlsx", FileMode.Open, FileAccess.Read);
  IWorkbook workbook = application.Workbooks.Open(inputStream, ExcelOpenType.Automatic);
  IWorksheet worksheet = workbook.Worksheets[0];

  //Print Columns 1 to 3 on every printed page
  worksheet.PageSetup.PrintTitleColumns = "$A$1:$C$65536";

  FileStream stream = new FileStream("TitleColumns.xlsx", FileMode.OpenOrCreate, FileAccess.ReadWrite);
  workbook.SaveAs(stream);
  workbook.Close();
  excelEngine.Dispose();
}
ExcelEngine excelEngine = new ExcelEngine();            
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Excel2013;
IWorkbook workbook = application.Workbooks.Open("Sample.xlsx", ExcelOpenType.Automatic);
IWorksheet worksheet = workbook.Worksheets[0];

//Print Columns 1 to 3 on every printed page
worksheet.PageSetup.PrintTitleColumns = "$A$1:$C$65536";
workbook.SaveAs("TitleColumns.xlsx");
workbook.Close();
excelEngine.Dispose();
Dim excelEngine As New ExcelEngine()
Dim application As IApplication = excelEngine.Excel
application.DefaultVersion = ExcelVersion.Excel2013
Dim workbook As IWorkbook = application.Workbooks.Open("Sample.xlsx", ExcelOpenType.Automatic)
Dim worksheet As IWorksheet = workbook.Worksheets(0)

'Print Columns 1 to 3 on every printed page
worksheet.PageSetup.PrintTitleColumns = "$A$1:$C$65536"
workbook.SaveAs("TitleColumns.xlsx")
workbook.Close()
excelEngine.Dispose()

For information on Print settings, refer to section Page Setup Settings.

See Also