public class WriteExcel
extends java.lang.Object
WriteExcel.Area
object is used to provide the columns of data and rows of columns with which to
populate the data. CellStyles
are provided by default, by column specification
or by row.
Multiple Areas can be applied to one sheet and multiple Sheets can be written using multiple Areas.
CellStyles
are created using the addStyle
or addStyles
methods.
WriteExcel
manages the combinations so only the active combinations are written to the the output Workbook CellStyle registry.
Modifier and Type | Class and Description |
---|---|
static class |
WriteExcel.Area
Used to specify the contents of a portion of a Sheet.
|
Constructor and Description |
---|
WriteExcel() |
Modifier and Type | Method and Description |
---|---|
WriteExcel |
addCellComment(java.lang.String sSheet,
int nRow,
int nCol,
java.lang.String sText)
Adds a Comment to the specified Cell using a fixed Font.
|
WriteExcel |
addCellComment(java.lang.String sSheet,
int nRow,
int nCol,
java.lang.String sText,
boolean bFixed)
Adds a Comment to the specified Cell with specified Font.
|
WriteExcel |
addDataFormat(java.lang.String sName,
java.lang.String sMatcher,
java.lang.String sDataFmt)
Adds a DataFormat type.
|
void |
addExternalSheet(java.lang.String sSheet,
Sheet oS,
java.lang.String sPrintArea)
This clones the source Sheet into the Workbook being constructed as a Sheet named
sSheet . |
WriteExcel |
addStyleDefn(java.lang.String sName,
java.lang.String sStyStr)
Registers a named style.
|
WriteExcel |
bookSheet(java.lang.String sName)
Creates an empty Sheet called sName.
|
java.lang.String |
cellSummary(Cell oC)
Return a stringized summary of the Cell.
|
void |
close()
Close the Workbook.
|
static WriteExcel |
create(Workbook oWB)
Internal access to Cell manipulation routines.
|
static WriteExcel |
create(WriteExcel oWE,
java.lang.String sFileName)
Creates an instance of
WriteExcel that will write sFileName. |
static WriteExcel |
create(WriteExcel oWE,
java.lang.String sFileName,
java.lang.String sSrcName)
Create an instance of
WriteExcel that will write sFileName and uses sSrcFile as a template file. |
WriteExcel.Area |
createArea(java.lang.String sSheet)
Creates an
Area with a base Row and Column of 0 on the specified Sheet. |
WriteExcel.Area |
createArea(java.lang.String sSheet,
int nRow,
int nCol)
Creates an
Area with the specified base Row and Column on the specified Sheet. |
void |
customExit(CellStyle oCS,
java.lang.String sStr)
Allow custom modifications to CellStyle.
|
java.lang.String[] |
dumpCellFontCache()
Return a stringized summary of the CellStyle Font cache.
|
java.lang.String[] |
dumpCellStyleCache()
Return a stringized summary of the CellStyle cache.
|
java.lang.String[] |
getHdrExtras()
Gets the standard header builtins styles.
|
int |
getRowCount(java.lang.String sSheet)
Gets the Row count on the specified Sheet.
|
java.lang.String[] |
getStdExtras()
Gets the standard builtins styles.
|
java.lang.String |
getStrValue(java.lang.String sSheet,
int nRow,
int nCol)
Gets the String value of the specified Cell.
|
Workbook |
getWorkbook()
Gets the
Workbook being constructed. |
void |
makeFileLink(java.lang.String sLinkSty,
java.lang.String sSheet,
int nRow,
int nCol,
java.lang.String sStr,
java.lang.String sFileName)
Make File link entry.
|
void |
makeIndexLink(java.lang.String sTargLinkSty,
java.lang.String sTargSheet,
java.lang.String sIdxName,
int nIdxRow,
int nIdxCol,
java.lang.String sIdxLinkSty)
Make index link entry.
|
void |
makeIndexLink(java.lang.String sTargLinkSty,
java.lang.String sTargSheet,
java.lang.String sIdxName,
int nIdxRow,
int nIdxCol,
java.lang.String sIdxLinkSty,
int nIdxLnkRow,
int nIdxLnkCol)
Make index link entry.
|
void |
makeStdLink(java.lang.String sTargLinkSty,
java.lang.String sTargSheet,
int nTargRow,
int nTargCol,
java.lang.String sSrcLinkSty,
java.lang.String sSrcSheet,
int nSrcRow,
int nSrcCol)
Make standard link entry.
|
void |
makeUniLink(java.lang.String sLinkSty,
java.lang.String sTargSheet,
int nTargRow,
int nTargCol,
java.lang.String sSrcSheet,
int nSrcRow,
int nSrcCol,
java.lang.String sSrcText)
Make a one directional link from the source location to the target location.
|
void |
makeUniLink(java.lang.String sLinkSty,
java.lang.String sTargSheet,
int nTargRow,
int nTargCol,
java.lang.String sSrcSheet,
int nSrcRow,
int nSrcCol,
java.lang.String sSrcText,
int nRows)
Make a one directional link from the source location to the target location.
|
void |
makeUrlLink(java.lang.String sLinkSty,
java.lang.String sSheet,
int nRow,
int nCol,
java.lang.String sStr,
java.lang.String sUrlName)
Make URL link entry.
|
void |
refreshCell(java.lang.String sSheet,
int nRow,
int nCol)
Refresh the calculated value in a specific Cell.
|
void |
refreshCells()
Refresh the calculated values of all cells.
|
void |
setNegativeFormat(boolean bShowNegAsRed,
java.lang.String sNegFmt)
Set how negative numbers are displayed.
|
void |
zapCell(java.lang.String sSheet,
int nRow,
int nCol,
java.lang.String sVal)
Make changes to specified Cell data with the CellStyle determined according to
sVal . |
void |
zapCell(java.lang.String sSheet,
int nRow,
int nCol,
java.lang.String sVal,
boolean bKeepStyle)
Make changes to specified Cell data with optional CellStyle preservation.
|
public WriteExcel addCellComment(java.lang.String sSheet, int nRow, int nCol, java.lang.String sText) throws java.lang.Exception
This method call should be after the WriteExcel.Area.writeArea()
has been issued.
sSheet
- The sheet name containing th cell.nRow
- The base 0 row index on the Sheet.nCol
- The base 0 column index on the Row.sText
- The comment text.
Use a "\r\n" to cause a line break.java.lang.Exception
public WriteExcel addCellComment(java.lang.String sSheet, int nRow, int nCol, java.lang.String sText, boolean bFixed) throws java.lang.Exception
This method call should be after the WriteExcel.Area.writeArea()
has been issued.
sSheet
- The sheet name containing th cell.nRow
- The base 0 row index on the Sheet.nCol
- The base 0 column index on the Row.sText
- The comment text.
Use a "\r\n" to cause a line break.bFont
- The text Font will be fixed if true, otherwise the default proportional font.java.lang.Exception
public WriteExcel addDataFormat(java.lang.String sName, java.lang.String sMatcher, java.lang.String sDataFmt) throws java.lang.Exception
Format Cells...
.
This method call(s) should be made before any Areas
are created using the createArea
methods.
Examples
The next line of code will display with no decimal points. The ND
is the signal.
It will match any number of decimals but there must be at least one.
addDataFormat("nodec","^ND-?[0-9]+[.][0-9]+$","0",)
A value of "ND10.333"
or "ND-40.1"
would select this format;
The next line of code will display with two decimal points and negative coloring. It will match any number of decimals but there must be at least one.
addDataFormat(neg-nodec","^NND-?[0-9]+[.][0-9]+$","0.00;[Red]0.00")
A value of "NND10.333"
or "NND-40.1"
would select this format;
sName
- The DataFormat name. Do not use names starting with "@" as these are internal name.
These names are only used in the logs created as style combinations
are registered.sMatcher
- The Regex string to match the value being written to the Cell. It should match the entire value.sDataFmt
- The value used as the DataFormat for the CellStyle.
When processing the pattern match, they are executed in the sequence of the calls to method and before any standard DataFormats seen at Standard Types.
The matched Cell data is assumed to be a double or integer with formatting aids such as $ and , puncuation marks and an optional prefix signal.. These are removed when the parsing for the double or integer value takes place.
Thus an alphabetic 'signal' string prefix can be used to coerce the match to select a particular formatting style.
java.lang.Exception
public void addExternalSheet(java.lang.String sSheet, Sheet oS, java.lang.String sPrintArea) throws java.lang.Exception
sSheet
.
This is primarily used to create new Sheets from a template. The source Sheet can be the same Workbook or from a Workbook
opened with ReadExcel
.
The cloning process prevserves the original column widths, merged cells and formatting.
The Print Area
, when specified, will be adjusted to refelect the new Sheet name. All Print Properties in the
original Sheet are also transcribed.
java.lang.Exception
public WriteExcel addStyleDefn(java.lang.String sName, java.lang.String sStyStr) throws java.lang.Exception
Names starting with '#' are builtin Style Names and should not be used unless purposefully overriding a builtin Style Name. They are listed at Builtin Style Names.
Names are case sensitive and can contain alphanumeric characters as well as the - and _ character.
Must be called before calls createArea
that reference the Style. Changes to a Style definition will be ignored
once it is referenced when a Cell using the style is written to the Workbook.
sName
- The name to register.sStyStr
- The cell style orders as described in Cell Style Orders Syntax.java.lang.Exception
public WriteExcel bookSheet(java.lang.String sName) throws java.lang.Exception
It can happen that the processing sequence is different than the preferred Sheet sequence in the resulting Workbook. Calling this method allows the Sheet to be created in the desired sequence.
java.lang.Exception
public java.lang.String cellSummary(Cell oC) throws java.lang.Exception
oC
- Cell to examinejava.lang.Exception
public java.lang.String[] dumpCellStyleCache() throws java.lang.Exception
java.lang.Exception
public void customExit(CellStyle oCS, java.lang.String sStr)
CE(...)
order.
It allows custom CellStyle changes to be made to the CellStyle being constructed that are not covered by the order set. It is called just prior to caching the CellStyle after all other orders have been processed.
oCS
- CellStyle
being constructed.sStr
- The string contained in the CE(...)
order. This allows different settings based on the string passed in.public java.lang.String[] dumpCellFontCache() throws java.lang.Exception
java.lang.Exception
public void close() throws java.lang.Exception
FileOutputStream
closed. No further changes can be made.java.lang.Exception
public static WriteExcel create(WriteExcel oWE, java.lang.String sFileName) throws java.lang.Exception
WriteExcel
that will write sFileName.oWE
- the parent instance that subclasses WriteExcel;sFileName
- The fully qualified file path and name suitable for use in a FileOutputStrem.java.lang.Exception
public static WriteExcel create(WriteExcel oWE, java.lang.String sFileName, java.lang.String sSrcName) throws java.lang.Exception
WriteExcel
that will write sFileName and uses sSrcFile as a template file.oWE
- the parent instance that subclasses WriteExcel;sFileName
- The fully qualified file path and name suitable for use in a FileOutputStrem.sSrcName
- The input .xlsx file that is to be used as a template.java.lang.Exception
public static WriteExcel create(Workbook oWB)
oWB
- The Workbook we are accessingpublic WriteExcel.Area createArea(java.lang.String sSheet) throws java.lang.Exception
Area
with a base Row and Column of 0 on the specified Sheet. The Sheet is created if it does not exist.sSheet
- The Sheet name for the Area
.Area
.java.lang.Exception
public WriteExcel.Area createArea(java.lang.String sSheet, int nRow, int nCol) throws java.lang.Exception
Area
with the specified base Row and Column on the specified Sheet. The Sheet is created if it does not exist.sSheet
- The Sheet name for the Area
.nRow
- The 0 based row index.nCol
- The 0 based column index.Area
.java.lang.Exception
public java.lang.String[] getHdrExtras()
Each String returned is of the format #name:stystr
where
name
is the style name prefixed with a #
to mark it as standard stylestystr
is the string orders as defined in CellStyle orders.public int getRowCount(java.lang.String sSheet)
sSheet
- The sheet name to inspect.public java.lang.String[] getStdExtras()
Each String returned is of the format #name:stystr
where
name
is the style name prefixed with a #
to mark it as standard stylestystr
is the string orders as defined in CellStyle orders.public java.lang.String getStrValue(java.lang.String sSheet, int nRow, int nCol) throws java.lang.Exception
sSheet
- The sheet name to inspect.nRow
- The base 0 row index on the Sheet.nCol
- The base 0 column index on the Row.null
.java.lang.Exception
public Workbook getWorkbook() throws java.lang.Exception
Workbook
being constructed.
This allows inspection or modifications to be made to the Workbook using the POI library directly. Caution is advised. Calling this method allows the Sheet to be created in the desired sequence.
java.lang.Exception
public void makeFileLink(java.lang.String sLinkSty, java.lang.String sSheet, int nRow, int nCol, java.lang.String sStr, java.lang.String sFileName) throws java.lang.Exception
The sheet must exist that will be linked before this method can be called.
sTargLinkSty
- Style of index to target link.
A null
value is treated as #lkc
.
The builtin #lkc
is centered underlined blue.
The builtin #lnk
is non-centered underlined blue.
Other style values may be used provided they have been added with the addStyleDefn
method
sSheet
- Source sheet name on which to create linksIdxRow
- Link row on source page.sIdxCol
- Link column on source page.sStr
- The link textsFileName
- The File path passed to the Windows routines.java.lang.Exception
public void makeIndexLink(java.lang.String sTargLinkSty, java.lang.String sTargSheet, java.lang.String sIdxName, int nIdxRow, int nIdxCol, java.lang.String sIdxLinkSty) throws java.lang.Exception
This method allows for the creation of an index page which is useful in complicated spreadsheets.
The sheet must exist that will be linked before this method can be called.
If necessary 'defer' logic should be added to the creating program as sheets are created
that need links. The bookSkeet
method can be called to position the "index" sheet as the first sheet in the Workbook.
It creates a bi-directional link to the target page from the index page by calling method
makeIndexLink
with nIdxLnkRow
=1 and nIdxLnkCol
=1.
sTargLinkSty
- Style of index to target link.
A null
value is treated as #lkc
.
The builtin #lkc
is centered underlined blue.
The builtin #lnk
is non-centered underlined blue.
Other style values may be used provided they have been added with the addStyleDefn
method
sTargSheet
- Sheet name to indexsIdxName
- Link name on index page.sIdxRow
- Link row on index page.sIdxCol
- Link column on index page.sIdxLinkSty
- Style of target to index link.
A null
value will use the sTargLinkSty
value or #lkc
.
The builtin #lkc
is centered underlined blue.
The builtin #lnc
is non-centered underlined blue.
Other style values may be used provided they have been added with the addStyleDefn
method
java.lang.Exception
public void makeIndexLink(java.lang.String sTargLinkSty, java.lang.String sTargSheet, java.lang.String sIdxName, int nIdxRow, int nIdxCol, java.lang.String sIdxLinkSty, int nIdxLnkRow, int nIdxLnkCol) throws java.lang.Exception
This method allows for the creation of an index page which is useful in complicated spreadsheets.
The sheet must exist that will be linked before this method can be called.
If necessary 'defer' logic should be added to the creating program as sheets are created
that need links. The bookSkeet
method can be called to position the "index" sheet as the first sheet in the Workbook.
It creates a bi-directional link to the target page from the index page.
It creates the index pointer on the target page at row nIdxLnkRow
column nIdxLnkCol
.
sTargLinkSty
- Style of index to target link.
A null
value is treated as #lkc
.
The builtin #lkc
is centered underlined blue.
The builtin #lnk
is non-centered underlined blue.
Other style values may be used provided they have been added with the addStyleDefn
method
sTargSheet
- Sheet name to indexsIdxName
- Link name on index page.sIdxRow
- Link row on index page.sIdxCol
- Link column on index page.sIdxLinkSty
- Style of target to index link.
A null
value will use the sTargLinkSty
value or #lkc
.
The builtin #lkc
is centered underlined blue.
The builtin #lnc
is non-centered underlined blue.
Other style values may be used provided they have been added with the addStyleDefn
method
sIdxLnkRow
- Link row on target page.sIdxLnkCol
- Link column on target page.java.lang.Exception
public void makeStdLink(java.lang.String sTargLinkSty, java.lang.String sTargSheet, int nTargRow, int nTargCol, java.lang.String sSrcLinkSty, java.lang.String sSrcSheet, int nSrcRow, int nSrcCol) throws java.lang.Exception
The sheets must exist that will be linked before this method can be called. If necessary 'defer' logic should be added to the creating program as sheets are created that need links.
The target and source parameters in this method are commutative.
sTargLinkSty
- Style of target to source link.
A null
value is treated as #lkc
.
The builtin #lkc
is centered underlined blue.
The builtin #lnk
is non-centered underlined blue.
Other style values may be used provided they have been added with the addStyleDefn
method
sTargSheet
- Target Sheet namesTargRow
- Link row on target page.sTargCol
- Link column on target page.sSrcLinkSty
- Style of source to link.
A null
value is treated as #lkc
.
The builtin #lkc
is centered underlined blue.
The builtin #lnk
is non-centered underlined blue.
Other style values may be used provided they have been added with the addStyleDefn
method
sSrcSheet
- Source Sheet namesSrcRow
- Link row on source page.sSrcCol
- Link column on source page.java.lang.Exception
public void makeUniLink(java.lang.String sLinkSty, java.lang.String sTargSheet, int nTargRow, int nTargCol, java.lang.String sSrcSheet, int nSrcRow, int nSrcCol, java.lang.String sSrcText) throws java.lang.Exception
The sheets must exist that will be linked before this method can be called. If necessary 'defer' logic should be added to the creating program as sheets are created that need links.
Implemented by calling makeUniLink
with an nRows
value of 1
.
sLinkSty
- Style of link.
A null
value is treated as #lkc
.
The builtin #lkc
is centered underlined blue.
The builtin #lnk
is non-centered underlined blue.
Other style values may be used provided they have been added with the addStyleDefn
method
sTargSheet
- Target Sheet namesTargRow
- Link row on target page.sTargCol
- Link column on target page.sSrcSheet
- Source Sheet namesSrcRow
- Link row on source page.sSrcCol
- Link column on source page.sSrcText
- Text value to put in link text on the source sheet.java.lang.Exception
public void makeUniLink(java.lang.String sLinkSty, java.lang.String sTargSheet, int nTargRow, int nTargCol, java.lang.String sSrcSheet, int nSrcRow, int nSrcCol, java.lang.String sSrcText, int nRows) throws java.lang.Exception
The sheets must exist that will be linked before this method can be called. If necessary 'defer' logic should be added to the creating program as sheets are created that need links.
When the link is pressed the target location is selected. Multiple rows are selected if the parameter nRows is greated than 1.
sLinkSty
- Style of link.
A null
value is treated as #lkc
.
The builtin #lkc
is centered underlined blue.
The builtin #lnk
is non-centered underlined blue.
Other style values may be used provided they have been added with the addStyleDefn
method
sTargSheet
- Target Sheet namesTargRow
- Link row on target page.sTargCol
- Link column on target page.sSrcSheet
- Source Sheet namesSrcRow
- Link row on source page.sSrcCol
- Link column on source page.sSrcText
- Text value to put in link text on the source sheet.nRows
- number of rows to select in target location.java.lang.Exception
public void makeUrlLink(java.lang.String sLinkSty, java.lang.String sSheet, int nRow, int nCol, java.lang.String sStr, java.lang.String sUrlName) throws java.lang.Exception
The sheet must exist that will be linked before this method can be called.
sTargLinkSty
- Style of index to target link.
A null
value is treated as #lkc
.
The builtin #lkc
is centered underlined blue.
The builtin #lnk
is non-centered underlined blue.
Other style values may be used provided they have been added with the addStyleDefn
method
sSheet
- Source sheet name on which to create linksIdxRow
- Link row on source page.sIdxCol
- Link column on source page.sStr
- The link textsUrlName
- The URL passed to the browserjava.lang.Exception
public void refreshCell(java.lang.String sSheet, int nRow, int nCol)
WriteExcel uses the POI library provided FormulaEvaluator to maintain the current values of cells containing formulae. This method invokes the FormulaEvaluator instance for this Workbook for the specific cell.
This should be used if referenced values have been changed before obtaining the current value of a cell that contains calculated values.
If the refenced Cell does not exist this call is ignored.
sSheet
- Sheet namenRow
- Row number relative to 0nCol
- Column number relative to 0public void refreshCells()
WriteExcel uses the POI library provided FormulaEvaluator to maintain the current values of cells containing formulae. This method invokes the FormulaEvaluator instance for this Workbook for all cells.
This should be used if referenced values have been changed before obtaining the current value of a cell that contains calculated values.
public void setNegativeFormat(boolean bShowNegAsRed, java.lang.String sNegFmt) throws java.lang.Exception
This can be changed for a specific format using addGenericType
with
an appropriate signal prefix as shown in the example.
Must be called before calls to addStyles
or createArea
.
bShowNegAsRed
- Negative numbers will also be shown in redsNegFmt
- Valid combinations as shown below:
java.lang.Exception
public void zapCell(java.lang.String sSheet, int nRow, int nCol, java.lang.String sVal) throws java.lang.Exception
sVal
.sSheet
- The sheet name containing the cell.nRow
- The base 0 row index on the Sheet.nCol
- The base 0 column index on the Row.sVal
- The replacement Cell text. This is parsed according to format specifier.java.lang.Exception
public void zapCell(java.lang.String sSheet, int nRow, int nCol, java.lang.String sVal, boolean bKeepStyle) throws java.lang.Exception
sSheet
- The sheet name containing the cell.nRow
- The base 0 row index on the Sheet.nCol
- The base 0 column index on the Row.sVal
- The replacement Cell text. This is parsed according to format specifier.
This is ignored if bKeepStyle
is true.boolean
- bKeepStyle The existing style format is preserved if true. Otherwise a new format is determined according to the parsing of sVal
.java.lang.Exception