Wednesday, 01 July 2020 03:47

Java set or remove page break on Excel worksheet

Written by  support iceblue
Rate this item
(0 votes)

This article will demonstrate how to add page break and remove page break for Excel worksheets on Java applications.

Add page break on Excel worksheet:

import com.spire.xls.*;

public class ExcelPageBreak {
    public static void main(String[] args) {
        //Create a Workbook and load the sample file 
        Workbook workbook = new Workbook();
        workbook.loadFromFile("Sample.xlsx");

        //Get the first worksheet
        Worksheet sheet = workbook.getWorksheets().get(0);

        //Set Excel Page Break Horizontally
        sheet.getHPageBreaks().add(sheet.getRange().get("A8"));
        sheet.getHPageBreaks().add(sheet.getRange().get("A14"));

        //Set Excel Page Break Vertically
        sheet.getVPageBreaks().add(sheet.getRange().get("B1"));
        sheet.getVPageBreaks().add(sheet.getRange().get("C1"));

        //Set view mode to Preview mode
        workbook.getWorksheets().get(0).setViewMode(ViewMode.Preview);

        //Save the document
        String output = "output/AddPageBreak.xlsx";
        workbook.saveToFile(output, ExcelVersion.Version2013);
    }
}

Effective screenshot after set Excel page break horizontally and vertically:

Java set or remove page break on Excel worksheet

Remove the page break:

import com.spire.xls.*;

public class ExcelPageBreak {
    public static void main(String[] args) {
        //Create a Workbook and load the sample file
        Workbook workbook = new Workbook();
        workbook.loadFromFile("output/AddPageBreak.xlsx");

        //Get the first worksheet
        Worksheet sheet = workbook.getWorksheets().get(0);

        //Clear all the vertical page breaks
        sheet.getVPageBreaks().clear();

        //Remove the first horizontal Page Break
        sheet.getHPageBreaks().removeAt(0);

        //Set the ViewMode as Preview mode
        sheet.setViewMode(ViewMode.Preview);

        //Save the document
        String output = "output/removePageBreak.xlsx";
        workbook.saveToFile(output, ExcelVersion.Version2013);
      }
}

Effective screenshot after remove the vertical page breaks and horizontal page break:

Java set or remove page break on Excel worksheet

Additional Info

  • tutorial_title: