Académique Documents
Professionnel Documents
Culture Documents
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();
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.*;
//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;
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 "";
}
int sheetIndex=wb.getSheetIndex(wsName);
if(sheetIndex==-1)
return null;
else{
int rowNum = retrieveNoOfRows(wsName);
int colNum = retrieveNoOfCols(wsName);
int colNumber=-1;
if(colNumber==-1){
return null;
}
return data;
}
}
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;
}
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;
if(colNumber==-1){
return false;
}
}catch(Exception e){
e.printStackTrace();
return false;
}
return true;
}
if(colNumber==-1){
return false;
}
cell.setCellValue(Result);
}catch(Exception e){
e.printStackTrace();
return false;
}
return true;
}
}