Working with Cell Borders and Alignment in XLSX Using POI in Java

By Arvind Rai, April 24, 2014
In this page we will learn how to work with border and text alignment for a cell of XLSX. Color of border can be changed and the text written inside it can be aligned simply by using POI API. We will discuss it with example.

Working with Cell Borders in XLSX

A cell has four border top, bottoms, left and right. The entire four borders can be given separate styles like color or border width. Better understand by below example.
CellBorders.java
package com.concretepage.poi;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.IndexedColors;
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.xssf.usermodel.XSSFWorkbook;
public class CellBorders {
	public static void main(String[] args) throws IOException {
	    Workbook workbook = new XSSFWorkbook();
	    Sheet sheet = workbook.createSheet("sheet1");

	    CellStyle cellStyle = workbook.createCellStyle();
	    cellStyle.setBorderBottom(CellStyle.BORDER_THICK);
	    cellStyle.setBottomBorderColor(IndexedColors.BLUE.getIndex());
	    cellStyle.setBorderLeft(CellStyle.BORDER_DASHED);
	    cellStyle.setLeftBorderColor(IndexedColors.GREEN.getIndex());
	    cellStyle.setBorderRight(CellStyle.BORDER_DOTTED);
	    cellStyle.setRightBorderColor(IndexedColors.RED.getIndex());
	    cellStyle.setBorderTop(CellStyle.BORDER_HAIR);
	    cellStyle.setTopBorderColor(IndexedColors.CORAL.getIndex());
	    
	    Row row = sheet.createRow(1);
	    Cell cell = row.createCell(1);
	    cell.setCellValue("TEST DATA");
	    cell.setCellStyle(cellStyle);

	    FileOutputStream fos =new FileOutputStream(new File("D:/xlsx/cp.xlsx"));
	    workbook.write(fos);
	    fos.close();
	    System.out.println("Done");
	}
} 
Run the example and check for excel. See that the entire four borders that have different styles.
Working with Cell Borders and Alignment in XLSX Using POI in Java

Working with Cell Alignment in XLSX

Cell alignment is another important part while writing data in excel. The text can be aligned in many ways. In the example we will see a sample demo for alignment.
CellAlignmentDemo.java
package com.concretepage.poi;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.ss.usermodel.Cell;
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.xssf.usermodel.XSSFWorkbook;
public class CellAlignmentDemo {
	public static void main(String[] args) throws IOException {
	    Workbook workbook = new XSSFWorkbook();
	    Sheet sheet = workbook.createSheet("sheet1");
	    
	    CellStyle cellStyle = workbook.createCellStyle();
	    cellStyle.setAlignment(CellStyle.ALIGN_CENTER);
            cellStyle.setVerticalAlignment(CellStyle.ALIGN_FILL);
	    
	    Row row = sheet.createRow(1);
	    Cell cell = row.createCell(1);
	    cell.setCellValue("ALLIGN TEST DATA");
	    cell.setCellStyle(cellStyle);

	    FileOutputStream fos =new FileOutputStream(new File("D:/xlsx/cp.xlsx"));
	    workbook.write(fos);
	    fos.close();
	    System.out.println("Done");
	}
} 
Run the example and see the excel file. You will see that the text written in cell has been aligned accordingly.
Working with Cell Borders and Alignment in XLSX Using POI in Java
POSTED BY
ARVIND RAI
ARVIND RAI
LEARN MORE








©2024 concretepage.com | Privacy Policy | Contact Us