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();
}
}
}
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();
}
}
}
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();
}
}
}
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();
}
}
}
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();
}
}
}
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();
}
}
}
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();
}
}
}
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();
}
}
}
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();
}
}
}
Happy Coding 😊
Related Articles
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