ComponentOne Excel for UWP
Excel for UWP Task-Based Help / Adding a Page Break to a Worksheet
In This Topic
    Adding a Page Break to a Worksheet
    In This Topic

    You can easily add page breaks in rows and columns for files in OpenXML (.xlsx) format using the XLColumn.PageBreak and XLRow.PageBreak properties.

    In XAML View

    1. Right-click References in the Solution Explorer and select Add Reference from the list.
    1. Browse to find C1.UWPl.Excel.dll.
    2. Click OK to add the assembly reference to your application.
    1. In XAML View, place your cursor between the <Grid> </Grid> tags.
    1. Add two standard Button controls and one standard TextBox control to the page.
    1. Edit the markup for the first button so that it resembles the following:
    Markup
    Copy Code
    <Button x:Name="HelloButton" Content="Click Hello" />
    
    1. Edit the markup for the second button so that it resembles the following:
    Markup
    Copy Code
    <Button x:Name="SaveButton" Content="Save" />
    
    1. Edit the markup for the TextBox control so that it resembles the following:
    Markup
    Copy Code
    <TextBox               
       Name="_tbContent"
       Text="Empty"
       IsReadOnly="True"
       AcceptsReturn="True"
       FontFamily="Courier New"
       Background="White" Margin="465,10,242,722" />
    
    1. Create  an event named HelloButton_Click for HelloButton and switch to the code view of MainPage.xaml. This will also add a HelloButton_Click event to the code.
    1. Switch back to Design View and double-click the SaveButton to add a SaveButton_Click event to the code. This will open the Code View.

    In Code View

    1. Add a using statement to the top of the page:
    C#
    Copy Code
    using C1.Xaml.Excel;
    
    1. Add the following code to the MainPage class so that it resembles the following:
    C#
    Copy Code
    public sealed partial class MainPage : Page
        {
            C1XLBook _book;
        }
    
    1. Create a C1XLBook by adding the following code to the InitializeComponent() method:
    C#
    Copy Code
    _book = new C1XLBook();
    
    1. Add the RefreshView() method. You will call this method later in the code:
    C#
    Copy Code
    void RefreshView()
            {
            }
    
    1. Add some text values and page breaks.
    C#
    Copy Code
    private void HelloButton_Click(object sender, RoutedEventArgs e)
            {
                // step 1: create a new workbook
                _book = new C1XLBook();
    
               // add text values and page breaks
               _book.Sheets[0][2, 3].Value = "page1";
               _book.Sheets[0].Rows[2].PageBreak = true;
               _book.Sheets[0][0, 1].Value = "test1";
               _book.Sheets[0][0, 2].Value = "test2";
               _book.Sheets[0].Columns[1].PageBreak = true;
               _book.Sheets[0][3, 3].Value = "page2";
          
                // step 2: allow user to save the file
                _tbContent.Text = "'Hello World' workbook has been created, you can save it now.";
                RefreshView();
            }
    
    1. Save the workbook.
    C#
    Copy Code
    async void SaveButton_Click(object sender, RoutedEventArgs e)
            {
                Debug.Assert(_book != null);
    
                var picker = new Windows.Storage.Pickers.FileSavePicker();
                picker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.DocumentsLibrary;
                picker.FileTypeChoices.Add("Open XML Excel file", new List<string>() { ".xlsx" });
                picker.FileTypeChoices.Add("BIFF Excel file", new List<string>() { ".xls" });
                picker.SuggestedFileName = "New Book";
    
                var file = await picker.PickSaveFileAsync();
                if (file != null)
                {
                    try
                    {
                        // step 1: save file
                        var fileFormat = Path.GetExtension(file.Path).Equals(".xls") ? FileFormat.Biff8 : FileFormat.OpenXml;
                        await _book.SaveAsync(file, fileFormat);
                        // step 2: user feedback
                        _tbContent.Text = string.Format("File has been saved to: {0}.", file.Path);
                        RefreshView();
                    }
                    catch (Exception x)
                    {
                        _tbContent.Text = string.Format("EXCEPTION: {0}", x.Message);
                    }
                }
            }
    

    When you run your application, you'll be able to save the file you create. In Excel, select the Page Layout tab, and select the Print checkbox under Gridlines. The worksheet should look similar to the following:

    See Also