Home Jsp Poi Selecting Excel Sheet File

Related Tutorials


 
 

Share on Google+Share on Google+

Selecting Excel Sheet File

Advertisement
In this program we are going to select the excel sheet using java .

selecting excel sheet file

     

In this program we are going to select the excel sheet using java .You can create any number of  sheets and you can set selected sheet . 

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

You can create any number of sheets and give the name of the sheet. You can set default selected sheet by using setSelected (Boolean value). The sheet which you want to select setSelected(True) and pass false for rest others sheets.

You can pass cells and rows values into sheets. And also you can set it selected.

In this example we have created six sheets and given its names and we setSelected(true) for  "third sheet" and setSelected( flase) for rest. The selected sheet will displayed as shown in output of program .

 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 contentType="application/vnd.ms-excel" %>
<%page import="java.io.*" %>
<%page import="java.util.*" %>
<%
try{
  HSSFWorkbook hwb = new HSSFWorkbook();
  HSSFSheet sheet1 = hwb.createSheet("new sheet");
  HSSFSheet sheet2 = hwb.createSheet("second sheet");
 HSSFSheet sheet = hwb.createSheet("third sheet");
  HSSFSheet sheet4 = hwb.createSheet("fourth sheet");
 HSSFSheet sheet5 = hwb.createSheet("third sheet");
  HSSFSheet sheet6 = hwb.createSheet("fifth sheet");
   sheet.setSelected(true);
   sheet1.setSelected(false);
 sheet2.setSelected(false);
 sheet6.setSelected(false);
 sheet4.setSelected(false);
 sheet5.setSelected(false);
FileOutputStream fileOut = new FileOutputStream
(
"c:\\excel\\selectingSheet.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.

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: May 19, 2007

Related Tutorials

Discuss: Selecting Excel Sheet File  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com