Home Jsp Poi Filling Color in Excel Using JSP



Filling Color in Excel Using JSP
Posted on: May 19, 2007 at 12:00 AM
In this program we are going to create a sheet and then fill background color ,pattern and forground color in the sheet.

filling color in excel using jsp

     

In this program we are going to create a sheet  and then  fill background color ,pattern and forground color in the sheet.
 
Code description 

The package we need to import is  java.io.*,java.util.* ,org.apache.poi.hssf.usermodel.HSSFSheet,
org.apache.poi.hssf.usermodel. HSSFColor,org.apache.poi.hssf.usermodel.HSSFCell,org.apache.poi.hssf.usermodel.
HSSFRow,org.apache.poi.hssf.usermodel
.HSSFCellstyle
and org.apache.poi.hssf.usermodel. HSSFWorkbook.

The method used in this example shift row
setFillForegroundColor(HSSFColor.ORANGE.index):
This method is used to fill the for ground color of the style sheet.

setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);:
This method is used to fill the pattern how it look like.

setFillBackgroundColor(HSSFColor.RED.index):
This method is used to fill the background color.

You can be used the following fields in place of above 
 
The color fields are:
HSSFColor.AQUA ,HSSFColor.BLACK,HSSFColor.BLUE ,HSSFColor.BLUE_GREY,HSSFColor.BRIGHT_GREEN,HSSFColor
 ,HSSFColor.BROWN, HSSFColor.CORAL ,HSSFColor.CORNFLOWER_BLUE,HSSFColor.DARK_BLUE ,HSSFColor.

 DARK_GREEN, HSSFColor. DARK_RED ,HSSFColor.DARK_TEAL,HSSFColor.DARK_YELLOW,HSSFColor.GOLD,
HSSFColor.GREEN ,  HSSFColor. GREY_25_PERCENT,HSSFColor.GREY_40_PERCENT ,HSSFColor.GREY_50_PERCENT,
  HSSFColor.GREY_80_PERCENT ,HSSFColor.INDIGO,HSSFColor.LAVENDER ,HSSFColor.LEMON_CHIFFON,HSSFColor.
 LIGHT_BLUE , HSSFColor .LIGHT_CORNFLOWER_BLUE,HSSFColor.LIGHT_GREEN ,HSSFColor.LIGHT_ORANGE,
  HSSFColor. LIGHT_TURQUOISE ,HSSFColor.LIGHT_YELLOW,HSSFColor. LIME,HSSFColor.MAROON,HSSFColor.
 OLIVE_GREEN ,HSSFColor.ORANGE,HSSFColor.ORCHID ,HSSFColor.PALE_BLUE,HSSFColor.PINK ,HSSFColor.PLUM,
 HSSFColor.RED , HSSFColor.ROSE,HSSFColor.ROYAL_BLUE ,HSSFColor.SEA_GREEN,HSSFColor.SKY_BLUE ,HSSFColor.
 TAN,HSSFColor. TEAL HSSFColor.TURQUOISE,HSSFColor.VIOLET ,HSSFColor.WHITE,HSSFColor.YELLOW

And the pattern fields are:
NO_FILL, SOLID_FOREGROUND, FINE_DOTS, ALT_BARS, SPARSE_DOTS, THICK_HORZ_BANDS, THICK_VERT_BANDS,
  THICK_BACKWARD_DIAG, THICK_FORWARD_DIAG, BIG_SPOTS, BRICKS, THIN_HORZ_BANDS, THIN_VERT_BANDS, THIN_BACKWARD_DIAG, THIN_FORWARD_DIAG, SQUARES, DIAMONDS .

In this example we are creating the sheet and four style sheet .Then we set the fill color of forground ,pattern and background color .Then at last we add the style sheet into cellls and finally we add the cells into sheet. The output of  the example is shown below

 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();
  for(int i=0;i<50;i++){
  HSSFRow row = sheet.createRow((short0+i);
  HSSFCell cell = row.createCell((short0);
  cell.setCellValue(1);
        style.setFillBackgroundColor(HSSFColor.AQUA.index);
  style.setFillPattern(HSSFCellStyle.BIG_SPOTS);
  style.setFillForegroundColor(HSSFColor.ORANGE.index);
  style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
  HSSFCell cell4 = row.createCell((short1);
    cell4.setCellValue(2);
  HSSFCell cell1 = row.createCell((short2);
  cell1.setCellValue(3);
    HSSFCell cell2 = row.createCell((short3);
  cell2.setCellValue(4);
  HSSFCell cell3 = row.createCell((short4);
  cell3.setCellValue(5);
  style.setFillBackgroundColor(HSSFColor.AQUA.index);
  style.setFillPattern(HSSFCellStyle.BIG_SPOTS);
  style1.setFillForegroundColor(HSSFColor.ORANGE.index);
  style1.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
  style2.setFillBackgroundColor(HSSFColor.RED.index);
      style2.setFillPattern(HSSFCellStyle.BIG_SPOTS);
  style3.setFillForegroundColor(HSSFColor.GREEN.index);
  style3.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
  style4.setFillForegroundColor(HSSFColor.BLUE.index);
  style4.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
  cell.setCellStyle(style);
    cell1.setCellStyle(style1);
  cell2.setCellStyle(style2);
  cell3.setCellStyle(style3);
  cell4.setCellStyle(style4);
  
 FileOutputStream fileOut = new FileOutputStream
("C:\\excel\\fillsColor.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.

Related Tags for Filling Color in Excel Using JSP:
cstylecolorbackgroundpatternintthiscellsetforexamplefinaladdtoroundexamfinallyastsheillastbackfilllscecellsinasmntsheetddadallintoackcreatingxaxampseecolatkisllmpleaandarttssthstatifinorgpleplndolo


More Tutorials from this section

Ask Questions?    Discuss: Filling Color in Excel Using JSP  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.