Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Working With Fonts 
 

In this program we are going to make an excel sheet and set the fonts of text values, set its size.

 

working with fonts

                         

In this program we are going to make an excel sheet and set the fonts of text values, set its size. We can also set the name of the font.

The packages we need to import are java.io.*,java.util.* ,org.apache.poi.hssf.usermodel.HSSFSheet,org.apache.poi. hssf.  usermodel. HSSFCellStyle, org.apache.poi.usermodel.HSSFRow, org.apache.poi.usermodel.HSSFCell , org.apache.poi.hssf.usermodel. HSSFWorkbook and org.apache.poi.hssf.usermodel.HSSFFont.

org.apache.poi.hssf.usermodel.HSSFFont class extends java.lang.Object and is used to represent a font used in workbook. This also  provides various methods to set various font properties on text.
  
In this example we are using following key points:

setFontHeightInPoints((short)value):
This method is used to set the height of the text.

setFontName("Courier New"):
This method is used to set the font name.

setItalic(true):
This method is used the to set text as Italic.

setStrikeout(true):
This method is used to set strikeout on text.

setFont(font):
This method is used to set font on cell style sheet. 

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.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");
    HSSFRow row = sheet.createRow((short1);
    HSSFCell cell = row.createCell((short1);
    cell.setCellValue("rajesh");
    HSSFFont font = hwb.createFont();
    font.setFontHeightInPoints((short)24);
    font.setFontName("Courier New");
    font.setItalic(true);
    font.setStrikeout(true);
    HSSFCellStyle cellStyle = hwb.createCellStyle();
    cellStyle.setFont(font);
    cell.setCellStyle(cellStyle);
    FileOutputStream fileOut = new FileOutputStream
(
"c:\\excel\\fontsExcel.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.

                         

» View all related tutorials
Related Tags: java c apache import model setup io client user print port anchor cli int ria package set oo simple boo

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.