Add Border to Excel Cell - Java POI Example Program

How to Add Border to Excel Cell in POI? - Introduction


In this tutorial, we will discuss how to add borders around your cells in an Excel workbook, using Java POI library. This formatting tip will help you to create borders for a cell, if you have a requirement to do so, with POI / Java. Download POI v3.8 to work with the examples provided in this post, and make sure you have all the JAR files in your class path. Let us get started with the examples now.


setBorderLeft, setBorderRight, setBorderTop, setBorderBottom


Wow, there are four different methods available in POI, through which you can precisely control the border on a cell, and set it to a pattern you want. Each of these methods offer different input parameters to be passed, that define the look and feel of your cell border. The method names convey what they are for, but we will add a brief note anyway:
  • setBorderLeft - Controls the appearance of left border.
  • setBorderRight - Customize right border with this method
  • setBorderTop - creates top border of your choice
  • setBorderBottom - Bottom border - customization

XLS - Create Excel Cell Border - Java Program Example


First let us deal with .XLS format. The methods are defined in HSSFCellStyle class, and we will see how to use them with an example program. The Java program that adds border to an excel cell, is provided below:

import java.io.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.hssf.usermodel.*;
public class AddCellBorder {  
        public static void main(String[] args) throws Exception{
                /* Create Workbook and Worksheet */
                HSSFWorkbook my_workbook = new HSSFWorkbook();
                HSSFSheet my_sheet = my_workbook.createSheet("Cell Borders");
                /* Get access to HSSFCellStyle */
                HSSFCellStyle my_style = my_workbook.createCellStyle();
                /* We are now ready to set borders for this style */
                /* Draw a thin left border */
                my_style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
                /* Add medium right border */
                my_style.setBorderRight(HSSFCellStyle.BORDER_MEDIUM);
                /* Add dashed top border */
                my_style.setBorderTop(HSSFCellStyle.BORDER_DASHED);
                /* Add dotted bottom border */
                my_style.setBorderBottom(HSSFCellStyle.BORDER_DOTTED);
                
                /* Let us create another style now */
                HSSFCellStyle my_style_2 = my_workbook.createCellStyle();
                
                /* Draw a thick left border */
                my_style_2.setBorderLeft(HSSFCellStyle.BORDER_THICK);
                /* Draw double lined right border */
                my_style_2.setBorderRight(HSSFCellStyle.BORDER_DOUBLE);
                /* Add dotted top border - hairy */
                my_style_2.setBorderTop(HSSFCellStyle.BORDER_HAIR);
                /* Add medium dashed bottom border */
                my_style_2.setBorderBottom(HSSFCellStyle.BORDER_MEDIUM_DASHED);
                
                /* One more style to illusrtate more border patterns */
                
                HSSFCellStyle my_style_3 = my_workbook.createCellStyle();
                
                /* border dash dotted*/
                my_style_3.setBorderLeft(HSSFCellStyle.BORDER_DASH_DOT);
                /* border medium dash dot */
                my_style_3.setBorderRight(HSSFCellStyle.BORDER_MEDIUM_DASH_DOT);
                /* border dash dot dot */
                my_style_3.setBorderTop(HSSFCellStyle.BORDER_DASH_DOT_DOT);
                /* border medium dash dot dot */
                my_style_3.setBorderBottom(HSSFCellStyle.BORDER_MEDIUM_DASH_DOT_DOT);
                
                /* Attach border styles to cell */              
                Row row = my_sheet.createRow(0);                
                Cell cell = row.createCell(0);
                cell.setCellValue("Add Border Example - 1 ");           
                cell.setCellStyle(my_style);
                
                row = my_sheet.createRow(1);
                row = my_sheet.createRow(2);
                cell = row.createCell(0);
                cell.setCellValue("Add Border Example - 2 ");           
                cell.setCellStyle(my_style_2);
                
                row = my_sheet.createRow(3);
                row = my_sheet.createRow(4);
                cell = row.createCell(0);
                cell.setCellValue("Add Border Example - 3 ");           
                cell.setCellStyle(my_style_3);
                
                        
                /* Write changes to the workbook */
                FileOutputStream out = new FileOutputStream(new File("C:\\cell_border_Example.xls"));
                my_workbook.write(out);
                out.close();
                
                
        }
}

XLS -Add Border - Example Output


We have described 12 different borders in a single program above. Here is an example output of the program.

Add Different Border to Excel Cell - Java POI Example Program - Output
Add Different Border to Excel Cell - Java POI Example Program - Output

 Add Cell Borders to XLSX File - POI Example Program


You can also add borders to .XLSX files using Apache POI. Just that you need to use different class files to handle XLSX files. In this case, we use XSSFCellStyle class to define cell borders. A sample program that adds different type of borders to XLSX files is given below:
import java.io.*;
import org.apache.poi.xssf.usermodel.*; 
import org.apache.poi.ss.usermodel.*;
public class AddCellBorderXLSX {  
        public static void main(String[] args) throws Exception{
                /* Create Workbook and Worksheet XLSX Format */
                XSSFWorkbook my_workbook = new XSSFWorkbook();
                XSSFSheet my_sheet = my_workbook.createSheet("XLSX Cell Border");
                /* Get access to XSSFCellStyle */
                XSSFCellStyle my_style = my_workbook.createCellStyle();         
                /* XLSX File borders now */
                /* Draw a thin left border */
                my_style.setBorderLeft(XSSFCellStyle.BORDER_THIN);
                /* Add medium right border */
                my_style.setBorderRight(XSSFCellStyle.BORDER_MEDIUM);
                /* Add dashed top border */
                my_style.setBorderTop(XSSFCellStyle.BORDER_DASHED);
                /* Add dotted bottom border */
                my_style.setBorderBottom(XSSFCellStyle.BORDER_DOTTED);
                
                /* Let us create another style now */
                XSSFCellStyle my_style_2 = my_workbook.createCellStyle();
                
                /* Draw a thick left border */
                my_style_2.setBorderLeft(XSSFCellStyle.BORDER_THICK);
                /* Draw double lined right border */
                my_style_2.setBorderRight(XSSFCellStyle.BORDER_DOUBLE);
                /* Add dotted top border - hairy */
                my_style_2.setBorderTop(XSSFCellStyle.BORDER_HAIR);
                /* Add medium dashed bottom border */
                my_style_2.setBorderBottom(XSSFCellStyle.BORDER_MEDIUM_DASHED);
                
                /* One more style to illusrtate more border patterns */
                
                XSSFCellStyle my_style_3 = my_workbook.createCellStyle();
                
                /* border dash dotted*/
                my_style_3.setBorderLeft(XSSFCellStyle.BORDER_DASH_DOT);
                /* border medium dash dot */
                my_style_3.setBorderRight(XSSFCellStyle.BORDER_MEDIUM_DASH_DOT);
                /* border dash dot dot */
                my_style_3.setBorderTop(XSSFCellStyle.BORDER_DASH_DOT_DOT);
                /* border medium dash dot dot */
                my_style_3.setBorderBottom(XSSFCellStyle.BORDER_MEDIUM_DASH_DOT_DOT);
                
                /* Attach border styles to cell */              
                Row row = my_sheet.createRow(0);                
                Cell cell = row.createCell(0);
                cell.setCellValue("Draw XLSX Cell Border Example - 1 ");                
                cell.setCellStyle(my_style);
                
                row = my_sheet.createRow(1);
                row = my_sheet.createRow(2);
                cell = row.createCell(0);
                cell.setCellValue("Draw XLSX Cell Border Example - 2 ");                
                cell.setCellStyle(my_style_2);
                
                row = my_sheet.createRow(3);
                row = my_sheet.createRow(4);
                cell = row.createCell(0);
                cell.setCellValue("Draw XLSX Cell Border Example - 3 ");                
                cell.setCellStyle(my_style_3);
                
                /* Write changes to the workbook */
                FileOutputStream out = new FileOutputStream(new File("C:\\cell_border_Example.xlsx"));
                my_workbook.write(out);
                out.close();            
        }
}

We have not covered one type of border, BORDER_SLANTED_DASH_DOT.  You can try this border pattern using the same program above. .If you have a question around cell borders, you can post it in the comments section.

No comments:

Post a Comment