Fit Hight Ad Wdth of Ecel Seet Uing JSP

In this program we are going to create a sheet after that set the values into it's cell and set the height and width.

Fit Hight Ad Wdth of Ecel Seet Uing JSP

fit height and width of excel sheet using jsp

     

In this program we are going to create a sheet after that set the values into it's cell and set the height and width. 

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.HSSFRow, org.apache.poi.hssf.usermodel.HSSFCell org.apache. poi.hssf.usermodel. HSSFSimpleShape and org.apache.poi.hssf.usermodel. HSSFWorkbook.
 
The method used in this example:
setFitHeight(short height):
This method is used to fit the height of the sheet.The return type is void.

setFitWidth(short width) :
This method is used to fit the width of the sheet .The return type o f this method is also void.

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 print setup then set cells values and finally set the fit height and width.   
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.HSSFPrintSetup"%>
<%page import="org.apache.poi.hssf.usermodel.HSSFRow"%>
<%page import="org.apache.poi.hssf.usermodel.HSSFCell"%>
<%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((short)1);
  HSSFCell cell = row.createCell((short)1);
  cell.setCellValue(111111);  
  HSSFPrintSetup ps = sheet.getPrintSetup();
  sheet.setAutobreaks(true);
  ps.setFitHeight((short)1);
  ps.setFitWidth((short)1);
FileOutputStream fileOut = new FileOutputStream
(
"c:\\excel\\fitSheetOnPage.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.