Set Font into TextBox String Using JSP

In this program we are going create a box and then after we pass the sting in the box.

Set Font into TextBox String Using JSP

set font into textbox string using jsp

     

In this program we are going create a box  and then after we pass the sting in the box. Finally we set the font for that string. 
Code description 
The package we need to import is java.io.*,java.util.* ,org.apache.poi.hssf.usermodel.HSSFSheet, org.apache.poi.hssf.usermodel. HSSFPrintSetup, org.apache.poi.hssf.usermodel.HSSFTextbox, org.apache.poi.hssf.usermodel.HSSFRichTextString, org.apache.poi.hssf.usermodel.HSSFPatriarch, org.apache.poi.hssf.usermodel.HSSFClientAnchor, org.apache. poi.hssf.usermodel. HSSFSimpleShape and org.apache.poi.hssf.usermodel. HSSFWorkbook.
 
The method used in this example:
applyFont(HSSFFont font):
This method is used to sets the font of the entire string.The return type of the method is void.

applyFont(int startIndex, int endIndex, HSSFFont font):
This method is used to applies a font to the specified range  of a string.

applyFont(int startIndex, int endIndex, short fontIndex):
This method is used to applies a font to the specified characters of a string.

 applyFont(short fontIndex):
This method is used to applies the specified font to the entire string.

createFont():
This method is used to create a new Font and add the font to the workbook,s font table.

In this example we are creating a sheet then we create the new font and add the font into workbook's object after that we create an object of patriarch after that we create textbox and create an object of string .We apply the font on string and finally add it.   
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.HSSFPatriarch"%>
<%page import="org.apache.poi.hssf.usermodel.HSSFClientAnchor"%>
<%page import="org.apache.poi.hssf.usermodel.HSSFTextbox"%>
<%page import="org.apache.poi.hssf.usermodel.HSSFRichTextString"%>
<%page import="org.apache.poi.hssf.usermodel.HSSFFont"%>
<%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");
  HSSFFont font = hwb.createFont();
  HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
  HSSFTextbox textbox1 = patriarch.createTextbox
(
new HSSFClientAnchor(0,0,0,0,(short)1,1,(short)2,2));
  HSSFRichTextString string = new HSSFRichTextString
(
"Rajesh Kumar");
  string.applyFont(2,5,font);
  textbox1.setString(string );
  FileOutputStream fileOut = new FileOutputStream
(
"c:\\excel\\setFontInBox.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.