public class Spreadsheet
extends java.lang.Object
implements java.io.Serializable
Modifier and Type | Class and Description |
---|---|
class |
Spreadsheet.CellRef
Inner class used to hold cell references (row & column) in a form.
|
class |
Spreadsheet.ColRef
Inner class used to hold column references in a form.
|
Modifier and Type | Field and Description |
---|---|
private static java.lang.StringBuffer |
A2Z
The letters of the alphabet from A through Z.
|
private org.apache.poi.ss.usermodel.CreationHelper |
ivCreateHelper
The instantiation helper for a workbook
|
private org.apache.poi.ss.usermodel.Row |
ivCurrentRow
The current spreadsheet row.
|
private int |
ivCurrentRowNum
The current spreadsheet row's number.
|
private org.apache.poi.ss.usermodel.Sheet |
ivCurrentSheet
The current sheet of the spreadsheet.
|
private int |
ivCurrentSheetNum
The current sheet index in the workbook.
|
private org.apache.poi.ss.usermodel.DataFormat |
ivDataFormat
The data format helper for a workbook
|
private org.apache.poi.ss.usermodel.CellStyle |
ivDateStyle
The default date style to use for cells that are not currently
formatted as a date
|
private java.text.DecimalFormat |
ivNumericCellToString
A formatter used when a numeric cell is formatted as a
String . |
private org.apache.poi.ss.usermodel.Drawing |
ivPatriarch
The drawing patriarch for the current sheet.
|
private org.apache.poi.ss.usermodel.CellStyle |
ivTimeStyle
The default time style to use for cells that are not currently
formatted as a time
|
private org.apache.poi.ss.usermodel.Workbook |
ivWorkBook
The main spreadsheet POI object.
|
private static long |
serialVersionUID
The class' serialization version id.
|
static int |
TYPE_XLS
Old-style XLS (HSSF) spreadsheet
|
static int |
TYPE_XLSX
XML-based XLSX (XSSF) spreadsheet
|
static int |
TYPE_XLSX_STREAMING
XML-based streaming XLSX (SXSSF) spreadsheet, small memory footprint with sliding window
|
Constructor and Description |
---|
Spreadsheet(java.io.File input)
Constructs a
Spreadsheet based on an existing Excel file. |
Spreadsheet(java.io.InputStream input)
Constructs a
Spreadsheet based on an existing Excel file. |
Spreadsheet(int type)
Constructs an empty
Spreadsheet . |
Modifier and Type | Method and Description |
---|---|
void |
addRow()
Adds a new row and sets the current row cursor to the new row.
|
void |
addSheet(java.lang.String name)
Adds a new sheet.
|
private void |
clearCellHighlight(org.apache.poi.ss.usermodel.Cell cell)
Clears the highlighted attribute for the cell.
|
void |
clearCellHighlight(java.lang.String cellRef)
Clears the highlighted attribute for the cell.
|
void |
clearColumnHighlight(java.lang.String colRef)
Clears the highlighted attribute for the column in the current row..
|
private org.apache.poi.ss.usermodel.CellStyle |
cloneCellStyle(org.apache.poi.ss.usermodel.Cell cell)
Clones a cells style.
|
static int |
convertColStringToNum(java.lang.String colRef)
Takes an column reference as named in Excel (i.e.
|
static java.lang.String |
convertNumToColString(int colNum)
Takes a 0 based column number and converts to an Excel column name
(i.e.
|
void |
deleteRow()
Removes the current row.
|
private org.apache.poi.ss.usermodel.Cell |
getCell(int rowNum,
int colNum,
boolean create)
Returns a cell given.
|
private org.apache.poi.ss.usermodel.Cell |
getCell(java.lang.String cellRef)
Returns a cell given an Excel style cell reference.
|
private org.apache.poi.ss.usermodel.Cell |
getCell(java.lang.String cellRef,
boolean create)
Returns a cell given an Excel style cell reference.
|
private java.lang.Object |
getCellValue(org.apache.poi.ss.usermodel.Cell cell)
Retrieves the value of the cell as a
Object . |
java.lang.Object |
getCellValue(java.lang.String cellRef)
Retrieves the value of the cell as a
Object . |
private boolean |
getCellValueBoolean(org.apache.poi.ss.usermodel.Cell cell)
Retrieves the value of the cell as a
boolean . |
boolean |
getCellValueBoolean(java.lang.String cellRef)
Retrieves the value of the cell as a
boolean . |
private java.util.Date |
getCellValueDate(org.apache.poi.ss.usermodel.Cell cell)
Retrieves the value of the cell as a
Date . |
java.util.Date |
getCellValueDate(java.lang.String cellRef)
Retrieves the value of the cell as a
Date . |
private double |
getCellValueDouble(org.apache.poi.ss.usermodel.Cell cell)
Retrieves the value of the cell as a
double . |
double |
getCellValueDouble(java.lang.String cellRef)
Retrieves the value of the cell as a
double . |
private java.lang.String |
getCellValueString(org.apache.poi.ss.usermodel.Cell cell)
Retrieves the value of the cell as a
String . |
java.lang.String |
getCellValueString(java.lang.String cellRef)
Retrieves the value of the cell as a
String . |
java.lang.String |
getColumnRef(int index)
Returns a column index's Excel column reference (i.e.
|
java.lang.Object |
getColumnValue(java.lang.String colRef)
Retrieves the value of a column in the current row as a
Object . |
boolean |
getColumnValueBoolean(java.lang.String colRef)
Retrieves the value of a column in the current row as a
boolean . |
java.util.Date |
getColumnValueDate(java.lang.String colRef)
Retrieves the value of a column in the current row as a
Date . |
double |
getColumnValueDouble(java.lang.String colRef)
Retrieves the value of a column in the current row as a
double . |
java.lang.String |
getColumnValueString(java.lang.String colRef)
Retrieves the value of a column in the current row as a
String . |
int |
getCurrentRow()
Returns the current row number as counted by Excel (i.e.
|
int |
getCurrentSheet()
Returns the current sheet number (index).
|
private org.apache.poi.ss.usermodel.Drawing |
getDrawingPatriarch()
Returns the current sheet's drawing patriarch.
|
int |
getLastCellNumCurrentRow()
Returns the last cell index (0 based) of the current row.
|
int |
getNumberOfSheets()
Returns the number of sheets in the workbook.
|
private java.text.DecimalFormat |
getNumericCellToStringFormatter()
A formatter used to convert a numeric cell type to a string
|
boolean |
hasMoreRows()
Determines if more rows exist after the current row.
|
boolean |
hasMoreSheets()
Determines if more sheets exist after the current sheet.
|
void |
nextRow()
Sets the current row to the row following the current row.
|
void |
nextSheet()
Sets the current sheet to the sheet following the current sheet.
|
void |
setCellComment(java.lang.String cellRef,
java.lang.String comment,
java.lang.String author)
Sets a comment for a cell.
|
private void |
setCellHighlight(org.apache.poi.ss.usermodel.Cell cell)
Highlight the cell.
|
void |
setCellHighlight(java.lang.String cellRef)
Highlight the cell.
|
private void |
setCellValue(org.apache.poi.ss.usermodel.Cell cell,
java.util.Date value)
Sets the value of a cell to a
Date . |
private void |
setCellValue(org.apache.poi.ss.usermodel.Cell cell,
java.sql.Time value)
Sets the value of a cell to a
Date . |
void |
setCellValue(java.lang.String cellRef,
java.util.Date value)
Set the value of the cell to a
Date . |
void |
setCellValue(java.lang.String cellRef,
double value)
Set the value of the cell to a
double . |
void |
setCellValue(java.lang.String cellRef,
java.lang.String value)
Set the value of the cell to a
String . |
void |
setColumnHighlight(java.lang.String colRef)
Highlight the column is the current row.
|
void |
setColumnImage(java.lang.String colRef,
java.io.File file)
Set the value of a column in the current row to an image.
|
void |
setColumnImage(java.lang.String colRef,
java.lang.String path)
Set the value of a column in the current row to an image.
|
void |
setColumnValue(java.lang.String colRef,
java.util.Date value)
Set the value of a column in the current row to a
Date . |
void |
setColumnValue(java.lang.String colRef,
double value)
Set the value of a column in the current row to a
double . |
void |
setColumnValue(java.lang.String colRef,
java.lang.String value)
Set the value of a column in the current row to a
String . |
void |
setColumnValue(java.lang.String colRef,
java.sql.Time value)
Set the value of a column in the current row to a
Date . |
void |
setCurrentRow(int rownum)
Sets the current row number using numbering used in Excel (i.e.
|
boolean |
setCurrentSheet(int index)
Sets the current sheet.
|
boolean |
setCurrentSheet(java.lang.String name)
Sets the current sheet.
|
void |
write(java.io.File out)
Writes the spreadsheet to a file.
|
void |
write(java.io.OutputStream out)
Writes the spreadsheet to a stream.
|
private static final long serialVersionUID
public static final int TYPE_XLS
public static final int TYPE_XLSX
public static final int TYPE_XLSX_STREAMING
private static final java.lang.StringBuffer A2Z
private java.text.DecimalFormat ivNumericCellToString
String
.
Cannot be defined as a static because decimal formatters are not thread safe.private org.apache.poi.ss.usermodel.Row ivCurrentRow
private int ivCurrentRowNum
private org.apache.poi.ss.usermodel.Sheet ivCurrentSheet
private int ivCurrentSheetNum
private org.apache.poi.ss.usermodel.Drawing ivPatriarch
private org.apache.poi.ss.usermodel.Workbook ivWorkBook
private org.apache.poi.ss.usermodel.CellStyle ivDateStyle
private org.apache.poi.ss.usermodel.CellStyle ivTimeStyle
private org.apache.poi.ss.usermodel.CreationHelper ivCreateHelper
private org.apache.poi.ss.usermodel.DataFormat ivDataFormat
public Spreadsheet(java.io.File input) throws java.io.IOException, org.apache.poi.openxml4j.exceptions.InvalidFormatException
Spreadsheet
based on an existing Excel file.input
- (File) File containing the existing XLS/XLSX file.java.io.IOException
- if the file cannot be read.org.apache.poi.openxml4j.exceptions.InvalidFormatException
- if the file is not a valid spreadsheet.public Spreadsheet(java.io.InputStream input) throws java.io.IOException, org.apache.poi.openxml4j.exceptions.InvalidFormatException
Spreadsheet
based on an existing Excel file.input
- (InputStream) Stream containing the existing XLS/XLSX data.java.io.IOException
- if the stream cannot be read.org.apache.poi.openxml4j.exceptions.InvalidFormatException
- if the inputstream is not a valid spreadsheet.public Spreadsheet(int type) throws java.io.IOException
Spreadsheet
. For use when a spread
sheet is not based on an existing file.type
- (int) The type of spreadsheet to create (XLS or XLSX).java.io.IOException
public void addSheet(java.lang.String name)
name
- (String) The name of the desired sheet within the
spreadsheet (case sensitive).public boolean setCurrentSheet(int index)
index
- (int) The index of the desired sheet within the
spreadsheet (0 based).true
if the current sheet set successfully,
otherwise false
.public boolean setCurrentSheet(java.lang.String name)
name
- (String) The name of the desired sheet within the
spreadsheet (case sensitive).true
if the current sheet set successfully,
otherwise false
.public int getCurrentSheet()
public int getNumberOfSheets()
public boolean hasMoreSheets()
true
if the more sheets exist after the
current sheet, otherwise false
.public void nextSheet()
public boolean getCellValueBoolean(java.lang.String cellRef)
boolean
. If the cell
type is not boolean, or can't be parsed as boolean, false is returned.cellRef
- (String) The cell reference using Excel terminology
(i.e. "D2" or "B25")public java.util.Date getCellValueDate(java.lang.String cellRef)
Date
.cellRef
- (String) The cell reference using Excel terminology
(i.e. "D2" or "B25")public double getCellValueDouble(java.lang.String cellRef)
double
. If the cell
type is not numeric, or can't be parsed as numeric, 0 is returned.cellRef
- (String) The cell reference using Excel terminology
(i.e. "D2" or "B25")public java.lang.String getCellValueString(java.lang.String cellRef)
String
. If the cell
type is numeric, the numeric value will be converted using an internally
defined decimal formatter.cellRef
- (String) The cell reference using Excel terminology
(i.e. "D2" or "B25")public java.lang.Object getCellValue(java.lang.String cellRef)
Object
. The returned
object could be a String, Date, Double
depending on the data
and cell format.cellRef
- (String) The cell reference using Excel terminology
(i.e. "D2" or "B25")public void setCellHighlight(java.lang.String cellRef)
cellRef
- (String) The cell reference using Excel terminology
(i.e. "D2" or "B25").public void clearCellHighlight(java.lang.String cellRef)
cellRef
- (String) The cell reference using Excel terminology
(i.e. "D2" or "B25").public void setCellValue(java.lang.String cellRef, java.lang.String value)
String
. If the cell does not
exist, it will be created.cellRef
- (String) The cell reference using Excel terminology
(i.e. "D2" or "B25").value
- (String) The new cell value.public void setCellValue(java.lang.String cellRef, double value)
double
. If the cell does not
exist, it will be created.cellRef
- (String) The cell reference using Excel terminology
(i.e. "D2" or "B25").value
- (double) The new cell value.public void setCellValue(java.lang.String cellRef, java.util.Date value)
Date
. If the cell does not
exist, it will be created.cellRef
- (String) The cell reference using Excel terminology
(i.e. "D2" or "B25").value
- (Date) The new cell value.public void setCellComment(java.lang.String cellRef, java.lang.String comment, java.lang.String author)
cellRef
- (String) The cell reference using Excel terminology
(i.e. "D2" or "B25")comment
- (String) The comment value.public int getCurrentRow()
public void setCurrentRow(int rownum)
rownum
- (int) The row number as known to Excel.public boolean hasMoreRows()
true
if the more rows exist after the
current row, otherwise false
.public int getLastCellNumCurrentRow()
public void nextRow()
public void addRow()
public void deleteRow()
public java.lang.String getColumnRef(int index)
index
- (int) A column index (zero based).public boolean getColumnValueBoolean(java.lang.String colRef)
boolean
.
The current row must be set. If the cell type is not boolean, or can't
be parsed as boolean, false is returned.colRef
- (String) The column reference using Excel terminology
(i.e. "D" or "B")public java.util.Date getColumnValueDate(java.lang.String colRef)
Date
.
The current row must be set.colRef
- (String) The column reference using Excel terminology
(i.e. "D" or "B")public double getColumnValueDouble(java.lang.String colRef)
double
.
The current row must be set. If the cell type is not numeric, or can't
be parsed as numeric, 0 is returned.colRef
- (String) The column reference using Excel terminology
(i.e. "D" or "B")public java.lang.String getColumnValueString(java.lang.String colRef)
String
.
The current row must be set. If the cell type is numeric, the numeric
value will be converted using an internally defined decimal formatter.colRef
- (String) The column reference using Excel terminology
(i.e. "D" or "B")public java.lang.Object getColumnValue(java.lang.String colRef)
Object
.
The current row must be set. The returned object could be a String,
Date, Double
depending on the data and cell format.colRef
- (String) The column reference using Excel terminology
(i.e. "D" or "B")public void setColumnValue(java.lang.String colRef, java.lang.String value)
String
. If
the cell does not exist, it will be created.colRef
- (String) The column reference using Excel terminology
(i.e. "D" or "B")value
- (String) The new cell value.public void setColumnValue(java.lang.String colRef, double value)
double
. If
the cell does not exist, it will be created.colRef
- (String) The column reference using Excel terminology
(i.e. "D" or "B")value
- (double) The new cell value.public void setColumnValue(java.lang.String colRef, java.sql.Time value)
Date
. If
the cell does not exist, it will be created.colRef
- (String) The column reference using Excel terminology
(i.e. "D" or "B")value
- (Date) The new cell value.public void setColumnValue(java.lang.String colRef, java.util.Date value)
Date
. If
the cell does not exist, it will be created.colRef
- (String) The column reference using Excel terminology
(i.e. "D" or "B")value
- (Date) The new cell value.public void setColumnHighlight(java.lang.String colRef)
colRef
- (String) The column reference using Excel terminology
(i.e. "D" or "B")public void clearColumnHighlight(java.lang.String colRef)
colRef
- (String) The column reference using Excel terminology
(i.e. "D" or "B")public void setColumnImage(java.lang.String colRef, java.lang.String path)
The supported image types are DIB, EMF, GIF, JPG, PICT, PNG and WMF.
Images should only be added to a new workbook (vs. to an existing workbook).
Unlike the setColumnValue
methods, there is no corresponding
getColumnImage
method.
colRef
- (String) The column reference using Excel terminology
(i.e. "D" or "B")path
- (String) The full path to an image file.public void setColumnImage(java.lang.String colRef, java.io.File file)
The supported image types are DIB, EMF, GIF, JPG, PICT, PNG and WMF.
Images should only be added to a new workbook (vs. to an existing workbook).
Unlike the setColumnValue
methods, there is no corresponding
getColumnImage
method.
colRef
- (String) The column reference using Excel terminology
(i.e. "D" or "B")file
- (File) The file object for the image file.public void write(java.io.File out) throws java.io.IOException
out
- (File) The file to write data to.java.io.IOException
- if the an error occurs during the write operation.public void write(java.io.OutputStream out) throws java.io.IOException
out
- (OutputStream) The stream to write data to.java.io.IOException
- if the an error occurs during the write operation.public static int convertColStringToNum(java.lang.String colRef)
colRef
- (String) The column reference using Excel terminology
(i.e. "D" or "B")public static java.lang.String convertNumToColString(int colNum)
colNum
- (int) The column index (0 based).private org.apache.poi.ss.usermodel.Cell getCell(java.lang.String cellRef)
cellRef
- (String) The cell reference using Excel terminology
(i.e. "D2" or "B25")private org.apache.poi.ss.usermodel.Cell getCell(java.lang.String cellRef, boolean create)
cellRef
- (String) The cell reference using Excel terminology
(i.e. "D2" or "B25")create
- (boolean) Flag determines if cell should be created if
it does not currently exist.private org.apache.poi.ss.usermodel.Cell getCell(int rowNum, int colNum, boolean create)
rowNum
- (int) Cell row number.colNum
- (int) Cell column number.create
- (boolean) Flag determines if cell should be created if
it does not currently exist.private boolean getCellValueBoolean(org.apache.poi.ss.usermodel.Cell cell)
boolean
.cell
- (Cell) The cell.private java.util.Date getCellValueDate(org.apache.poi.ss.usermodel.Cell cell)
Date
.cell
- (Cell) The cell.private double getCellValueDouble(org.apache.poi.ss.usermodel.Cell cell)
double
. If the cell
type is not numeric, or can't be parsed as numeric, 0 is returned.cell
- (Cell) The cell.private java.lang.String getCellValueString(org.apache.poi.ss.usermodel.Cell cell)
String
. If the cell
type is numeric, the numeric value will be converted using an internally
defined decimal formatter.cell
- (Cell) The cell.private java.lang.Object getCellValue(org.apache.poi.ss.usermodel.Cell cell)
Object
. The returned
object could be a String, Date, Double
depending on the data
and cell format.cell
- (Cell) The cell.private void setCellValue(org.apache.poi.ss.usermodel.Cell cell, java.util.Date value)
Date
.cell
- (Cell) The cell.value
- (Date) The new cell value.private void setCellValue(org.apache.poi.ss.usermodel.Cell cell, java.sql.Time value)
Date
.cell
- (Cell) The cell.value
- (Time) The new cell value.private void setCellHighlight(org.apache.poi.ss.usermodel.Cell cell)
cell
- (Cell) The cell.private org.apache.poi.ss.usermodel.CellStyle cloneCellStyle(org.apache.poi.ss.usermodel.Cell cell)
cell
- (Cell) The cell.private void clearCellHighlight(org.apache.poi.ss.usermodel.Cell cell)
cell
- (Cell) The cell.private org.apache.poi.ss.usermodel.Drawing getDrawingPatriarch()
private java.text.DecimalFormat getNumericCellToStringFormatter()