Page Setup

The sections below look at the sections of the Excel Page Setup dialog and the equivalent rust_xlsxwriter methods.

For more, general, information about the page setup options in Excel see the Microsoft documentation on Page Setup.

Page Setup - Page

The page Setup "Page" dialog looks like this:

Image of Page Setup Dialog Page section

The equivalent rust_xlsxwriter methods are:

  1. worksheet.set_portrait()
  2. worksheet.set_landscape()
  3. worksheet.set_print_scale()
  4. worksheet.set_print_fit_to_pages()
  5. worksheet.set_print_first_page_number()

Note, for worksheet.set_print_fit_to_pages() a common requirement is to fit the printed output to n pages wide but have the height be as long as necessary. To achieve this set the height to 0:

// SPDX-License-Identifier: MIT OR Apache-2.0
//
// Copyright 2022-2024, John McNamara, jmcnamara@cpan.org

//! The following example demonstrates setting the scale of the worksheet to fit
//! a defined number of pages vertically and horizontally. This example shows a
//! common use case which is to fit the printed output to 1 page wide but have
//! the height be as long as necessary.

use rust_xlsxwriter::{Workbook, XlsxError};

fn main() -> Result<(), XlsxError> {
    let mut workbook = Workbook::new();

    // Add a worksheet to the workbook.
    let worksheet = workbook.add_worksheet();

    // Set the printed output to fit 1 page wide and as long as necessary.
    worksheet.set_print_fit_to_pages(1, 0);

    workbook.save("worksheet.xlsx")?;

    Ok(())
}

Page Setup - Margins

The page Setup "Margins" dialog looks like this:

Image of Page Setup Dialog Margins section

The equivalent rust_xlsxwriter methods are:

  1. worksheet.set_margins()
  2. worksheet.set_print_center_horizontally()
  3. worksheet.set_print_center_vertically()

Page Setup - Header/Footer

The page Setup "Header/Footer" dialog looks like this:

Image of Page Setup Dialog Header/Footer section

The equivalent rust_xlsxwriter methods are:

  1. worksheet.set_header()
  2. worksheet.set_footer()
  3. worksheet.set_header_footer_scale_with_doc()
  4. worksheet.set_header_footer_align_with_page()

Headers and footers are explained in more detail in the next section on Adding Headers and Footers.

Note, the options for different first, odd and even pages are not supported in rust_xlsxwriter.

Page Setup - Sheet

The page Setup "Sheet" dialog looks like this:

Image of Page Setup Dialog Sheet section

The equivalent rust_xlsxwriter methods are:

  1. worksheet.set_print_area()
  2. worksheet.set_repeat_rows()
  3. worksheet.set_repeat_columns()
  4. worksheet.set_print_gridlines()
  5. worksheet.set_print_black_and_white()
  6. worksheet.set_print_draft()
  7. worksheet.set_print_headings()
  8. worksheet.set_page_order()