DEV Community


Posted on

Set Excel Print Page Margins Using Java

This article will share how to set Excel page margins before printing the Excel worksheets by using Free Spire.XLS for Java. With this 3rd party free API, we could set top margin, bottom margin, left margin, right margin, header margin, and footer margin.

Installation (2 Method)
1# Download the free API and unzip it, then add the Spire.Xls.jar file to your project as dependency.
2# You can also add the jar dependency to maven project by adding the following configurations to the pom.xml.

Enter fullscreen mode Exit fullscreen mode

Sample Code

import com.spire.xls.*;
import java.awt.*;

public class AddBorders {
    public static void main(String[] args){
        //Create a workbook instance
        Workbook workbook = new Workbook();
        //Load the Excel file

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

        //Get the cell range that you want to apply border style
        CellRange cr = sheet.getCellRange(sheet.getFirstRow(), sheet.getFirstColumn(), sheet.getLastRow(), sheet.getLastColumn());

        //Apply border style

        //Save the resultant file
        workbook.saveToFile("SetBorder.xlsx", ExcelVersion.Version2010);
Enter fullscreen mode Exit fullscreen mode


Discussion (0)