Selecting Excel Sheet File

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

Selecting Excel Sheet File

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.