Apache POI Vertical and Horizontal Excel Cell Alignment

Tags: Apache POI

Java Code Examples for:

  • org.apache.poi.ss.usermodel.HorizontalAlignment
  • org.apache.poi.ss.usermodel.VerticalAlignment

There are nine examples below to show how to use Apache POI library to create Excel cells with different aligment.

Example Left Top Excel Cell Alignment

package simplesolution.dev;

import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class CellStyleAlignmentLeftTopExample {

    public static void main(String... args) {
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("testing");
        sheet.setColumnWidth(0, 10000);
        Row row = sheet.createRow(0);
        row.setHeightInPoints(100);
        Cell cell = row.createCell(0);
        cell.setCellValue("SimpleSolution.dev");

        CellStyle cellStyle = workbook.createCellStyle();
        cellStyle.setAlignment(HorizontalAlignment.LEFT);
        cellStyle.setVerticalAlignment(VerticalAlignment.TOP);
        cell.setCellStyle(cellStyle);

        try(OutputStream outputStream = new FileOutputStream("sample-left-top-alignment.xlsx")) {
            workbook.write(outputStream);
        } catch(IOException ex) {
            ex.printStackTrace();
        }
    }
}
Excel output file for left top cell alignment:

Apache POI Excel Cell Left Top Alignment

Example Center Top Excel Cell Alignment

package simplesolution.dev;

import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class CellStyleAlignmentCenterTopExample {

    public static void main(String... args) {
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("testing");
        sheet.setColumnWidth(0, 10000);
        Row row = sheet.createRow(0);
        row.setHeightInPoints(100);
        Cell cell = row.createCell(0);
        cell.setCellValue("SimpleSolution.dev");

        CellStyle cellStyle = workbook.createCellStyle();
        cellStyle.setAlignment(HorizontalAlignment.CENTER);
        cellStyle.setVerticalAlignment(VerticalAlignment.TOP);
        cell.setCellStyle(cellStyle);

        try(OutputStream outputStream = new FileOutputStream("sample-center-top-alignment.xlsx")) {
            workbook.write(outputStream);
        } catch(IOException ex) {
            ex.printStackTrace();
        }
    }
}
Excel output file for center top cell alignment:

Apache POI Excel Cell Center Top Alignment

Example Right Top Excel Cell Alignment

package simplesolution.dev;

import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class CellStyleAlignmentRightTopExample {

    public static void main(String... args) {
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("testing");
        sheet.setColumnWidth(0, 10000);
        Row row = sheet.createRow(0);
        row.setHeightInPoints(100);
        Cell cell = row.createCell(0);
        cell.setCellValue("SimpleSolution.dev");

        CellStyle cellStyle = workbook.createCellStyle();
        cellStyle.setAlignment(HorizontalAlignment.RIGHT);
        cellStyle.setVerticalAlignment(VerticalAlignment.TOP);
        cell.setCellStyle(cellStyle);

        try(OutputStream outputStream = new FileOutputStream("sample-right-top-alignment.xlsx")) {
            workbook.write(outputStream);
        } catch(IOException ex) {
            ex.printStackTrace();
        }
    }
}
Excel output file for right top cell alignment:

Apache POI Excel Cell Right Top Alignment

Example Right Middle Excel Cell Alignment

package simplesolution.dev;

import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class CellStyleAlignmentRightCenterExample {

    public static void main(String... args) {
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("testing");
        sheet.setColumnWidth(0, 10000);
        Row row = sheet.createRow(0);
        row.setHeightInPoints(100);
        Cell cell = row.createCell(0);
        cell.setCellValue("SimpleSolution.dev");

        CellStyle cellStyle = workbook.createCellStyle();
        cellStyle.setAlignment(HorizontalAlignment.RIGHT);
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        cell.setCellStyle(cellStyle);

        try(OutputStream outputStream = new FileOutputStream("sample-right-center-alignment.xlsx")) {
            workbook.write(outputStream);
        } catch(IOException ex) {
            ex.printStackTrace();
        }
    }
}
Excel output file for right center cell alignment:

Apache POI Excel Cell Right Center Alignment

Example Right Bottom Excel Cell Alignment

package simplesolution.dev;

import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class CellStyleAlignmentRightBottomExample {

    public static void main(String... args) {
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("testing");
        sheet.setColumnWidth(0, 10000);
        Row row = sheet.createRow(0);
        row.setHeightInPoints(100);
        Cell cell = row.createCell(0);
        cell.setCellValue("SimpleSolution.dev");

        CellStyle cellStyle = workbook.createCellStyle();
        cellStyle.setAlignment(HorizontalAlignment.RIGHT);
        cellStyle.setVerticalAlignment(VerticalAlignment.BOTTOM);
        cell.setCellStyle(cellStyle);

        try(OutputStream outputStream = new FileOutputStream("sample-right-bottom-alignment.xlsx")) {
            workbook.write(outputStream);
        } catch(IOException ex) {
            ex.printStackTrace();
        }
    }
}
Excel output file for right botttom cell alignment:

Apache POI Excel Cell Right Bottom Alignment

Example Center Bottom Excel Cell Alignment

package simplesolution.dev;

import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class CellStyleAlignmentCenterBottomExample {

    public static void main(String... args) {
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("testing");
        sheet.setColumnWidth(0, 10000);
        Row row = sheet.createRow(0);
        row.setHeightInPoints(100);
        Cell cell = row.createCell(0);
        cell.setCellValue("SimpleSolution.dev");

        CellStyle cellStyle = workbook.createCellStyle();
        cellStyle.setAlignment(HorizontalAlignment.CENTER);
        cellStyle.setVerticalAlignment(VerticalAlignment.BOTTOM);
        cell.setCellStyle(cellStyle);

        try(OutputStream outputStream = new FileOutputStream("sample-center-bottom-alignment.xlsx")) {
            workbook.write(outputStream);
        } catch(IOException ex) {
            ex.printStackTrace();
        }
    }
}
Excel output file for center botttom cell alignment:

Apache POI Excel Cell Center Bottom Alignment

Example Left Bottom Excel Cell Alignment

package simplesolution.dev;

import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class CellStyleAlignmentLeftBottomExample {

    public static void main(String... args) {
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("testing");
        sheet.setColumnWidth(0, 10000);
        Row row = sheet.createRow(0);
        row.setHeightInPoints(100);
        Cell cell = row.createCell(0);
        cell.setCellValue("SimpleSolution.dev");

        CellStyle cellStyle = workbook.createCellStyle();
        cellStyle.setAlignment(HorizontalAlignment.LEFT);
        cellStyle.setVerticalAlignment(VerticalAlignment.BOTTOM);
        cell.setCellStyle(cellStyle);

        try(OutputStream outputStream = new FileOutputStream("sample-left-bottom-alignment.xlsx")) {
            workbook.write(outputStream);
        } catch(IOException ex) {
            ex.printStackTrace();
        }
    }
}
Excel output file for left botttom cell alignment:

Apache POI Excel Cell Left Bottom Alignment

Example Left Middle Excel Cell Alignment

package simplesolution.dev;

import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class CellStyleAlignmentLeftCenterExample {

    public static void main(String... args) {
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("testing");
        sheet.setColumnWidth(0, 10000);
        Row row = sheet.createRow(0);
        row.setHeightInPoints(100);
        Cell cell = row.createCell(0);
        cell.setCellValue("SimpleSolution.dev");

        CellStyle cellStyle = workbook.createCellStyle();
        cellStyle.setAlignment(HorizontalAlignment.LEFT);
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        cell.setCellStyle(cellStyle);

        try(OutputStream outputStream = new FileOutputStream("sample-left-center-alignment.xlsx")) {
            workbook.write(outputStream);
        }catch(IOException ex) {
            ex.printStackTrace();
        }
    }
}
Excel output file for left center cell alignment:

Apache POI Excel Left Center Alignment

Example Center Excel Cell Alignment

package simplesolution.dev;

import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class CellStyleAlignmentCenterExample {

    public static void main(String... args) {
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("testing");
        sheet.setColumnWidth(0, 10000);
        Row row = sheet.createRow(0);
        row.setHeightInPoints(100);
        Cell cell = row.createCell(0);
        cell.setCellValue("SimpleSolution.dev");

        CellStyle cellStyle = workbook.createCellStyle();
        cellStyle.setAlignment(HorizontalAlignment.CENTER);
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        cell.setCellStyle(cellStyle);

        try(OutputStream outputStream = new FileOutputStream("sample-center-alignment.xlsx")) {
            workbook.write(outputStream);
        }catch(IOException ex) {
            ex.printStackTrace();
        }
    }
}
Excel output file for center cell alignment:

Apache POI Excel Center Alignment

Happy Coding 😊

Apache POI Excel Cell Fonts

Apache POI Excel Cell Border and Border Color

Apache POI Excel Cell Background Foreground and Font Color

Apache POI Merging Excel Cells

Java Create Excel File .xlsx using Apache POI

Java Read Excel File using Apache POI

Java Read Excel Workbook from File using Apache POI

Java Read Excel Workbook from InputStream using Apache POI

Java Read Password Protected Excel File using Apache POI

Java How to Iterate over Sheets Rows and Cells of Excel file using Apache POI

Java Add Rows to Existing Excel File using Apache POI

Java Add Sheet to Existing Excel File using Apache POI

Java Remove Sheet from Existing Excel File using Apache POI

Java Create Formula Excel Cells using Apache POI