Vous êtes sur la page 1sur 10

HSSF - Horrible Sytle Sheet Format:

int getNumberOfSheets()
get the number of spreadsheets
in the workbook (this will be
three after serialization)
getSheet(java.lang.String name)
HSSFSheet Get sheet with the given name
(case insensitive match)
getSheetAt(int index)
HSSFSheet Get the HSSFSheet object at the
given index.
getSheetIndex(Sheet sheet)
int Returns the index of the given
sheet
getSheetIndex(java.lang.String name)
int Returns the index of the sheet by
his name
getSheetName(int sheetIndex)
java.lang.String
Get the sheet name
setActiveSheet(int index)
void Convenience method to set the
active sheet.
sheetIterator()
java.util.Iterator<Sheet> Returns an iterator of the sheets
in the workbook in sheet order.
write()
Write out this workbook to the
void currently open File via the
writeablePOIFSFileSystem it was
opened as.
write(java.io.File newFile)
void Method write - write out this
workbook to a new File.
write(java.io.OutputStream stream)
void Method write - write out this
workbook to an OutputStream.
Constructor Detail
HSSFWorkbook
public HSSFWorkbook()
Creates new HSSFWorkbook from scratch
HSSFWorkbook
public HSSFWorkbook(java.io.InputStream s)
throws java.io.IOException
Companion to HSSFWorkbook(POIFSFileSystem), this constructs the POI filesystem around
your InputStream, including all nodes.
This calls HSSFWorkbook(InputStream, boolean) with preserve nodes set to true.
Throws:
java.io.IOException - if the stream cannot be read

cloneSheet
public HSSFSheet cloneSheet(int sheetIndex)
create an HSSFSheet from an existing sheet in the HSSFWorkbook.
Specified by:
cloneSheet in interface Workbook
Returns:
HSSFSheet representing the cloned sheet.
createSheet
public HSSFSheet createSheet(java.lang.String sheetname)
Create a new sheet for this Workbook and return the high level representation. Use this to create
new sheets.
Note that Excel allows sheet names up to 31 chars in length but other applications (such as
OpenOffice) allow more. Some versions of Excel crash with names longer than 31 chars, others -
truncate such names to 31 character.
POI's SpreadsheetAPI silently truncates the input argument to 31 characters. Example:

Sheet sheet = workbook.createSheet("My very long sheet name which is longer than 31 chars");
// will be truncated
assert 31 == sheet.getSheetName().length();
assert "My very long sheet name which i" == sheet.getSheetName();

Except the 31-character constraint, Excel applies some other rules:


Sheet name MUST be unique in the workbook and MUST NOT contain the any of the following
characters:
0x0000
0x0003
colon (:)
backslash (\)
asterisk (*)
question mark (?)
forward slash (/)
opening square bracket ([)
closing square bracket (])
The string MUST NOT begin or end with the single quote (') character.
Specified by:
createSheet in interface Workbook
Parameters:
sheetname - sheetname to set for the sheet.
Returns:
Sheet representing the new sheet.
Throws:
java.lang.IllegalArgumentException - if the name is null or invalid or workbook already contains a
sheet with this name
See Also:
WorkbookUtil.createSafeSheetName(String nameProposal)

sheetIterator
public java.util.Iterator<Sheet> sheetIterator()
Returns an iterator of the sheets in the workbook in sheet order. Includes hidden and very hidden
sheets.
Specified by:
sheetIterator in interface Workbook
Returns:
an iterator of the sheets.

iterator
public java.util.Iterator<Sheet> iterator()
Alias for sheetIterator() to allow foreach loops
Specified by:
iterator in interface java.lang.Iterable<Sheet>
getNumberOfSheets
public int getNumberOfSheets()
get the number of spreadsheets in the workbook (this will be three after serialization)
Specified by:
getNumberOfSheets in interface Workbook
Returns:
number of sheets
getSheetAt
public HSSFSheet getSheetAt(int index)
Get the HSSFSheet object at the given index.
Specified by:
getSheetAt in interface Workbook
Parameters:
index - of the sheet number (0-based physical & logical)
Returns:
HSSFSheet at the provided index
getSheet
public HSSFSheet getSheet(java.lang.String name)
Get sheet with the given name (case insensitive match)
Specified by:
getSheet in interface Workbook
Parameters:
name - of the sheet
Returns:
HSSFSheet with the name provided or null if it does not exist
removeSheetAt
public void removeSheetAt(int index)
Removes sheet at the given index.
Care must be taken if the removed sheet is the currently active or only selected sheet in the
workbook. There are a few situations when Excel must have a selection and/or active sheet. (For
example when printing - see Bug 40414).
This method makes sure that if the removed sheet was active, another sheet will become active in
its place. Furthermore, if the removed sheet was the only selected sheet, another sheet will become
selected. The newly active/selected sheet will have the same index, or one less if the removed sheet
was the last in the workbook.
Specified by:
removeSheetAt in interface Workbook
Parameters:
index - of the sheet (0-based)
close
public void close()
throws java.io.IOException
Closes the underlying NPOIFSFileSystem from which the Workbook was read, if any.
Once this has been called, no further operations, updates or reads should be performed on the
Workbook.
write
public void write(java.io.OutputStream stream)
throws java.io.IOException
Method write - write out this workbook to an OutputStream. Constructs a new POI POIFSFileSystem,
passes in the workbook binary representation and writes it out. If stream is a FileOutputStream on a
networked drive or has a high cost/latency associated with each written byte, consider wrapping the
OutputStream in a BufferedOutputStream to improve write performance.
Specified by:
write in interface Workbook
Specified by:
write in class POIDocument
Parameters:
stream - - the java OutputStream you wish to write the XLS to
Throws:
java.io.IOException - if anything can't be written.

package com.stta.utility;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;

public class Read_XLS {


public String filelocation;
public FileInputStream ipstr = null;
public FileOutputStream opstr =null;
private HSSFWorkbook wb = null;
private HSSFSheet ws = null;

public Read_XLS(String filelocation) {


this.filelocation=filelocation;
try {
ipstr = new FileInputStream(filelocation);
wb = new HSSFWorkbook(ipstr);
ws = wb.getSheetAt(0);
ipstr.close();
} catch (Exception e) {
e.printStackTrace();
}
}

//To retrieve No Of Rows from .xls file's sheets.


public int retrieveNoOfRows(String wsName){
int sheetIndex=wb.getSheetIndex(wsName);
if(sheetIndex==-1)
return 0;
else{
ws = wb.getSheetAt(sheetIndex);
int rowCount=ws.getLastRowNum()+1;
return rowCount;
}
}

//To retrieve No Of Columns from .cls file's sheets.


public int retrieveNoOfCols(String wsName){
int sheetIndex=wb.getSheetIndex(wsName);
if(sheetIndex==-1)
return 0;
else{
ws = wb.getSheetAt(sheetIndex);
int colCount=ws.getRow(0).getLastCellNum();
return colCount;
}
}

//To retrieve SuiteToRun and CaseToRun flag of test suite and test case.
public String retrieveToRunFlag(String wsName, String colName, String rowName){

int sheetIndex=wb.getSheetIndex(wsName);
if(sheetIndex==-1)
return null;
else{
int rowNum = retrieveNoOfRows(wsName);
int colNum = retrieveNoOfCols(wsName);
int colNumber=-1;
int rowNumber=-1;

HSSFRow Suiterow = ws.getRow(0);

for(int i=0; i<colNum; i++){


if(Suiterow.getCell(i).getStringCellValue().equals(colName.trim())){
colNumber=i;
}
}

if(colNumber==-1){
return "";
}
for(int j=0; j<rowNum; j++){
HSSFRow Suitecol = ws.getRow(j);
if(Suitecol.getCell(0).getStringCellValue().equals(rowName.trim())){
rowNumber=j;
}
}

if(rowNumber==-1){
return "";
}

HSSFRow row = ws.getRow(rowNumber);


HSSFCell cell = row.getCell(colNumber);
if(cell==null){
return "";
}
String value = cellToString(cell);
return value;
}
}

//To retrieve DataToRun flag of test data.


public String[] retrieveToRunFlagTestData(String wsName, String colName){

int sheetIndex=wb.getSheetIndex(wsName);
if(sheetIndex==-1)
return null;
else{
int rowNum = retrieveNoOfRows(wsName);
int colNum = retrieveNoOfCols(wsName);
int colNumber=-1;

HSSFRow Suiterow = ws.getRow(0);


String data[] = new String[rowNum-1];
for(int i=0; i<colNum; i++){
if(Suiterow.getCell(i).getStringCellValue().equals(colName.trim())){
colNumber=i;
}
}

if(colNumber==-1){
return null;
}

for(int j=0; j<rowNum-1; j++){


HSSFRow Row = ws.getRow(j+1);
if(Row==null){
data[j] = "";
}
else{
HSSFCell cell = Row.getCell(colNumber);
if(cell==null){
data[j] = "";
}
else{
String value = cellToString(cell);
data[j] = value;
}
}
}

return data;
}
}

//To retrieve test data from test case data sheets.


public Object[][] retrieveTestData(String wsName){
int sheetIndex=wb.getSheetIndex(wsName);
if(sheetIndex==-1)
return null;
else{
int rowNum = retrieveNoOfRows(wsName);
int colNum = retrieveNoOfCols(wsName);

Object data[][] = new Object[rowNum-1][colNum-2];

for (int i=0; i<rowNum-1; i++){


HSSFRow row = ws.getRow(i+1);
for(int j=0; j< colNum-2; j++){

if(row==null){
data[i][j] = "";
}
else{
HSSFCell cell = row.getCell(j);

if(cell==null){
data[i][j] = "";

}
else{

cell.setCellType(Cell.CELL_TYPE_STRING);
String value = cellToString(cell);
data[i][j] = value;

}
}
}
}
return data;
}

public static String cellToString(HSSFCell cell){


int type;
Object result;
type = cell.getCellType();
switch (type){
case 0 :
result = cell.getNumericCellValue();
break;

case 1 :
result = cell.getStringCellValue();
break;

default :
throw new RuntimeException("Unsupportd cell.");

}
return result.toString();
}

//To write result In test data and test case list sheet.
public boolean writeResult(String wsName, String colName, int rowNumber, String Result){
try{
int sheetIndex=wb.getSheetIndex(wsName);
if(sheetIndex==-1)
return false;
int colNum = retrieveNoOfCols(wsName);
int colNumber=-1;

HSSFRow Suiterow = ws.getRow(0);


for(int i=0; i<colNum; i++){
if(Suiterow.getCell(i).getStringCellValue().equals(colName.trim())){
colNumber=i;
}
}

if(colNumber==-1){
return false;
}

HSSFRow Row = ws.getRow(rowNumber);


HSSFCell cell = Row.getCell(colNumber);
if (cell == null)
cell = Row.createCell(colNumber);
cell.setCellValue(Result);

opstr = new FileOutputStream(filelocation);


wb.write(opstr);
opstr.close();

}catch(Exception e){
e.printStackTrace();
return false;
}
return true;
}

//To write result In test suite list sheet.


public boolean writeResult(String wsName, String colName, String rowName, String Result){
try{
int rowNum = retrieveNoOfRows(wsName);
int rowNumber=-1;
int sheetIndex=wb.getSheetIndex(wsName);
if(sheetIndex==-1)
return false;
int colNum = retrieveNoOfCols(wsName);
int colNumber=-1;

HSSFRow Suiterow = ws.getRow(0);


for(int i=0; i<colNum; i++){
if(Suiterow.getCell(i).getStringCellValue().equals(colName.trim())){
colNumber=i;
}
}

if(colNumber==-1){
return false;
}

for (int i=0; i<rowNum-1; i++){


HSSFRow row = ws.getRow(i+1);
HSSFCell cell = row.getCell(0);
cell.setCellType(Cell.CELL_TYPE_STRING);
String value = cellToString(cell);
if(value.equals(rowName)){
rowNumber=i+1;
break;
}
}

HSSFRow Row = ws.getRow(rowNumber);


HSSFCell cell = Row.getCell(colNumber);
if (cell == null)
cell = Row.createCell(colNumber);

cell.setCellValue(Result);

opstr = new FileOutputStream(filelocation);


wb.write(opstr);
opstr.close();

}catch(Exception e){
e.printStackTrace();
return false;
}
return true;
}
}

Vous aimerez peut-être aussi