public class ReadExcelFile
extends java.lang.Object
Methods are provided that can return selected rows of data from each sheet. The ReadExcelRecord class
is used to provide selection capabilities as to what rows are to be returned and what content of each row is to be returned.
This selection capability is used to create a regression test facility for the WriteExcel set of methods.
The ReadClassFile class is also used to point to a sheet that can be cloned using the method.
public static class OpenBalRec extends ReadExcelRecord {
static Matcher oM = Pattern.compile("^[ALS][0-9]{3}$").matcher("");
public String[] getColMap() {
return "3=sLab;2=sName;4=dVal".split("/");
}
public String sLab;
public String sName;
public double dVal;
@Override
public boolean canAccept() throws Exception {
if (this.sLab == null) return false;
if (oM.reset(this.sLab).find()) return true;
return false;
}
}
private void demoReader() throws Exception {
oREF = new ReadExcelFile();
oREF.openFile(oOpt.sInp);
ReadExcelRecord[] oRows = oREF.readExcelSmart("sample-bal-template",OpenBalRec.class,1,true);
for(ReadExcelRecord oRER:oRows) {
OpenBalRec oOBR = (OpenBalRec)oRER;
log(String.format("%s %10.2f %s",oOBR.sLab,oOBR.dVal,oOBR.sName));
}
oREF.closeFile();
}
| Modifier and Type | Class and Description |
|---|---|
static class |
ReadExcelFile.ColMap
The ColMap class describes a single column used internally as part of the
readExcel method processing. |
| Constructor and Description |
|---|
ReadExcelFile() |
| Modifier and Type | Method and Description |
|---|---|
void |
attachWorkbook(Workbook oWB)
Attaches the Workbook to a ReadExcelFile instance so all its methods can be used.
|
void |
closeFile()
Closes the Workbook.
|
java.lang.String |
getColAsStr(Row oRow,
ReadExcelFile.ColMap oCM)
Return a stringized version of the Cell at the specified location.
|
Cell |
getColCell(Row oRow,
ReadExcelFile.ColMap oCM)
Return Cell at the specified location.
|
java.lang.String |
getPrintArea(java.lang.String sSheet)
Returns the String version of a Sheet print area.
|
Sheet |
getSheet(java.lang.String sSheet)
Returns the named Sheet.
|
Workbook |
getWorkbook()
Gets the
Workbook being accessed. |
void |
openFile(java.lang.String sFile)
Opens the Workbook.
|
ReadExcelRecord[] |
readExcel(java.lang.String sSheet,
java.lang.Class<ReadExcelRecord> oRecCls)
Iterate across a sheet and selectively create a set of
readExcelRecord instances, one for each row selected. |
ReadExcelRecord[] |
readExcelSmart(java.lang.String sSheet,
java.lang.Class<?> oRecCls,
int nStartRow,
boolean bSkipBlank)
Iterate across a sheet and selectively create a set of
readExcelRecord instances, one for each row selected. |
public void openFile(java.lang.String sFile)
throws java.lang.Exception
sFile - the name of the file.java.lang.Exception - file does not exist or is not a valid Workbookpublic void attachWorkbook(Workbook oWB)
openFile method when a Workbook is known (such as in an ongoing WriteExcel operation.public void closeFile()
throws java.lang.Exception
It is ignored if the Workbook is part of
an active WriteExcel operation attached to this instance via attachWorkbook method.
java.lang.Exception - An internal operation throws an Exception.public Sheet getSheet(java.lang.String sSheet)
public java.lang.String getPrintArea(java.lang.String sSheet)
throws java.lang.Exception
java.lang.Exception - The sheet is not foundpublic ReadExcelRecord[] readExcel(java.lang.String sSheet, java.lang.Class<ReadExcelRecord> oRecCls) throws java.lang.Exception
readExcelRecord instances, one for each row selected.
It is implemented by calling readExcelSmart with a value of 0 for nStartRow and the value of
bSkipBlank set to false.
sSheet - the sheet name to processoRecCls - The class type used to instantiate each row readExcelRecord instance.java.lang.Exception - The sheet is not foundpublic ReadExcelRecord[] readExcelSmart(java.lang.String sSheet, java.lang.Class<?> oRecCls, int nStartRow, boolean bSkipBlank) throws java.lang.Exception
readExcelRecord instances, one for each row selected.
The nStartRow parameter is used to indicate the first row to start processing at. Its primary use is to
skip headers frequently found at the top of sheets.
The bSkipBlank will cause rows to be skipped in which the first field of the ColMap object has no Cell object.
sSheet - the sheet name to processoRecCls - The class type used to instantiate each row readExcelRecord instance.nStartRow - The first row to process relative to 0; This allows thebSkipBlank - Rows where the first field is null are skipped.java.lang.Exception - The sheet is not foundpublic java.lang.String getColAsStr(Row oRow,
ReadExcelFile.ColMap oCM)
throws java.lang.Exception
ReadExcelFile.ColMap parameter within the oRow parameter is the one processed.oRow - The row to process which can be obtained via the getSheet method.oCM - The column to select.java.lang.Exceptionpublic Cell getColCell(Row oRow,
ReadExcelFile.ColMap oCM)
throws java.lang.Exception
ReadExcelFile.ColMap parameter within the oRow parameter is the returned.oRow - The row to process which can be obtained via the getSheet method.oCM - The column to select.java.lang.Exceptionpublic Workbook getWorkbook()
throws java.lang.Exception
Workbook being accessed.
This allows inspection or modifications to be made to the Workbook using the POI library directly.
java.lang.Exception