different borders

In this program we are going create a sheet after that enter the values in that cell then we create different borders and we also sets the different colors.

different borders

different borders

     

In this program we are going create a sheet  after that  enter the values in that cell then we create different borders and we also sets the different colors. .
Code description 
The package we need to import is java.io.*,java.util.* ,org.apache.poi.hssf.usermodel.HSSFSheet,org.apache.poi.hssf.usermodel.
HSSFCell,org.apache.poi.hssf.usermodel.
HSSFCellStyle,org.apache.poi.hssf.usermodel.HSSFRow, org.apache.poi.usermodel. HSSFColor
and org.apache.poi.hssf.usermodel. HSSFWorkbook.
 
We can set  various borders and set various color of the borders.
The method used in this example shift row

setBorderLeft(short border):
This method is used to set the type of border to use for the left border of the cell In this method we pass only one parameter. That is border type. 

setBorderRight(short border):
This method is used to set the type of border to use for the right border of the cell 

 setBorderTop(short border):
This method is used to set the type of border to use for the top border of the cell 

setBorderBottom(short border):
This method is used to set the type of border to use for the bottom border of the cell 

 setLeftBorderColor(short color):
This method is used to set the color to use for the left border 

 setRightBorderColor(short color):
This method is used to set the color to use for the right border 

 setTopBorderColor(short color):
This method is used to set the color to use for the top border 

setBottomBorderColor(short color):
This method is used to set the color to use for the bottom border 

We can pass following short  type for border.
BORDER_NONE, BORDER_THIN, BORDER_MEDIUM, BORDER_DASHED, BORDER_DOTTED, BORDER_THICK, BORDER_DOUBLE, BORDER_HAIR, BORDER_MEDIUM_DASHED, BORDER_DASH_DOT, BORDER_MEDIUM_DASH_DOT, BORDER_DASH_DOT_DOT, BORDER_MEDIUM_DASH_DOT_DOT, BORDER_SLANTED_DASH_DOT

 The code of the program is given below:

<%page import="org.apache.poi.hssf.usermodel.HSSFSheet"%>
<%page import="org.apache.poi.hssf.usermodel.HSSFWorkbook"%>
<%page import="org.apache.poi.hssf.usermodel.HSSFRow"%>
<%page import="org.apache.poi.hssf.usermodel.HSSFCell"%>
<%page import="org.apache.poi.hssf.usermodel.HSSFCellStyle"%>
<%page import="org.apache.poi.hssf.util.HSSFColor"%>
<%page contentType="application/vnd.ms-excel" %>
<%page import="java.io.*" %>
<%page import="java.util.*" %>
<%
try{
  HSSFWorkbook hwb = new HSSFWorkbook();
  HSSFSheet sheet = hwb.createSheet("new sheet");
  HSSFCellStyle style1 = hwb.createCellStyle();
  HSSFCellStyle style2 = hwb.createCellStyle();
  HSSFCellStyle style3 = hwb.createCellStyle();
  HSSFCellStyle style4 = hwb.createCellStyle();
  HSSFCellStyle style = hwb.createCellStyle();
  HSSFRow row = sheet.createRow((short1);
  HSSFCell cell = row.createCell((short0);
  cell.setCellValue(1);
  style.setBorderRight(HSSFCellStyle.BORDER_THIN);
  style.setRightBorderColor(HSSFColor.BLUE.index);
  HSSFCell cell4 = row.createCell((short1);
  cell4.setCellValue(2);
  style1.setBorderTop(HSSFCellStyle.BORDER_MEDIUM_DASHED);
  style1.setTopBorderColor(HSSFColor.BLACK.index);
  HSSFCell cell1 = row.createCell((short2);
  cell1.setCellValue(3);
  style2.setBorderBottom(HSSFCellStyle.BORDER_THIN);
  style2.setBottomBorderColor(HSSFColor.BLACK.index);
  HSSFCell cell2 = row.createCell((short3);
  cell2.setCellValue(4);
  HSSFCell cell3 = row.createCell((short4);
  cell3.setCellValue(5);
  style3.setBorderBottom(HSSFCellStyle.BORDER_THIN);
  style3.setBottomBorderColor(HSSFColor.BLACK.index);
  style4.setBorderBottom(HSSFCellStyle.BORDER_THIN);
  style4.setBottomBorderColor(HSSFColor.BLACK.index);
  style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
  style.setLeftBorderColor(HSSFColor.GREEN.index);  
  cell.setCellStyle(style);
  cell1.setCellStyle(style1);
  cell2.setCellStyle(style2);
  cell3.setCellStyle(style3);
  cell4.setCellStyle(style4);
 FileOutputStream fileOut = new FileOutputStream
(
"C:\\excel\\border.xls");
  hwb.write(fileOut);
  fileOut.close();
  out.println("Your excel file has been generated"); 
  catch Exception ex ) {  
  } 


%>

The output of the program is given below:

Download this example.