Package com.github.javaxcel.out
Class AbstractExcelWriter<W extends org.apache.poi.ss.usermodel.Workbook,T>
java.lang.Object
com.github.javaxcel.out.AbstractExcelWriter<W,T>
- All Implemented Interfaces:
ExcelWriter<T>
- Direct Known Subclasses:
MapWriter
,ModelWriter
public abstract class AbstractExcelWriter<W extends org.apache.poi.ss.usermodel.Workbook,T> extends Object implements ExcelWriter<T>
Abstract excel writer
write(OutputStream, List)
beforeWrite(OutputStream, List)
createHeader(Sheet)
ifHeaderNamesAreEmpty(List)
writeToSheet(Sheet, List)
getNumOfColumns()
save(OutputStream)
afterWrite(OutputStream, List)
-
Field Summary
Fields Modifier and Type Field Description protected org.apache.poi.ss.usermodel.CellStyle[]
bodyStyles
protected List<String>
headerNames
Header's names.protected org.apache.poi.ss.usermodel.CellStyle[]
headerStyles
protected boolean
rolling
When the number of rows to be written is greater than maximum rows of sheet, whether to write excess rows to the next sheet.protected String
sheetName
Prefix of all the sheets' names.protected boolean
willAutoResize
protected boolean
willHideCols
protected boolean
willHideRows
protected W
workbook
Apache POI workbook. -
Constructor Summary
Constructors Modifier Constructor Description protected
AbstractExcelWriter(W workbook)
-
Method Summary
Modifier and Type Method Description protected void
afterWrite(OutputStream out, List<T> list)
AbstractExcelWriter<W,T>
autoResizeCols()
protected void
beforeWrite(OutputStream out, List<T> list)
AbstractExcelWriter<W,T>
bodyStyle(com.github.javaxcel.styler.ExcelStyleConfig config)
AbstractExcelWriter<W,T>
bodyStyles(com.github.javaxcel.styler.ExcelStyleConfig... configs)
AbstractExcelWriter<W,T>
defaultValue(String defaultValue)
Sets default value when value to be written is null or empty.AbstractExcelWriter<W,T>
disableRolling()
Disables rolling excess rows.protected abstract int
getNumOfColumns()
Returns the number of columns.AbstractExcelWriter<W,T>
headerNames(List<String> headerNames)
Sets header names.AbstractExcelWriter<W,T>
headerStyle(com.github.javaxcel.styler.ExcelStyleConfig config)
AbstractExcelWriter<W,T>
headerStyles(com.github.javaxcel.styler.ExcelStyleConfig... configs)
AbstractExcelWriter<W,T>
hideExtraCols()
AbstractExcelWriter<W,T>
hideExtraRows()
protected abstract void
ifHeaderNamesAreEmpty(List<String> headerNames)
Handles header names, if they are empty.AbstractExcelWriter<W,T>
sheetName(String sheetName)
Sets sheet name.void
write(OutputStream out, List<T> list)
Writes the data in the excel file.protected abstract void
writeToSheet(org.apache.poi.ss.usermodel.Sheet sheet, List<T> list)
Writes list of models to the sheet.
-
Field Details
-
workbook
Apache POI workbook.- See Also:
HSSFWorkbook
,XSSFWorkbook
,SXSSFWorkbook
-
headerNames
Header's names. -
sheetName
Prefix of all the sheets' names.- See Also:
sheetName(String)
-
rolling
protected boolean rollingWhen the number of rows to be written is greater than maximum rows of sheet, whether to write excess rows to the next sheet.Default is
true
.- See Also:
disableRolling()
-
headerStyles
protected org.apache.poi.ss.usermodel.CellStyle[] headerStyles -
bodyStyles
protected org.apache.poi.ss.usermodel.CellStyle[] bodyStyles -
willAutoResize
protected boolean willAutoResize- See Also:
autoResizeCols()
-
willHideRows
protected boolean willHideRows- See Also:
hideExtraRows()
-
willHideCols
protected boolean willHideCols- See Also:
hideExtraCols()
-
-
Constructor Details
-
Method Details
-
defaultValue
Sets default value when value to be written is null or empty.- Parameters:
defaultValue
- replacement of the value when it is null or empty string.- Returns:
AbstractExcelWriter
-
sheetName
Sets sheet name.Prefix for each sheet name. For example, if you set 'SHEET' to this, the names you can see are SHEET0, SHEET1, SHEET2, ...
If you invoke
disableRolling()
, the sheet name has no suffix. You can see the sheet name like this SHEET- Parameters:
sheetName
- sheet name- Returns:
AbstractExcelWriter
- See Also:
disableRolling()
-
headerNames
Sets header names.For example, the following list will be exported.
[ { "serialNumber": 10000, "name": "Choco cereal", "apiId": "2a60-4973-aec0-685e", "width": null, "depth": 0.0, "height": 20.5, "weight": 580.5 }, { "serialNumber": 10001, "name": "Oatmeal cereal", "apiId": "f15d-384d-0a4b-97ec", "width": 10.2, "depth": 4.0, "height": 6.0, "weight": 575.0 } ]
To change the header names, place the names you want them changed to in the order like this.
List<String> names = Arrays.asList( "SERIAL_NUMBER", "NAME", "API_ID", "WIDTH" "DEPTH", "HEIGHT", "WEIGHT"); ExcelWriterFactory.create(new SXSSFWorkbook(), Product.class) .headerNames(names) .write(new FileOutputStream(file), list);
Then the header names will be changed you want.
+---------------+----------------+---------------------+-------+-------+--------+--------+ | SERIAL_NUMBER | NAME | API_ID | WIDTH | DEPTH | HEIGHT | WEIGHT | +---------------+----------------+---------------------+-------+-------+--------+--------+ | 10000 | Choco cereal | 2a60-4973-aec0-685e | | 0.0 | 20.5 | 580.5 | +---------------+----------------+---------------------+-------+-------+--------+--------+ | 10001 | Oatmeal cereal | f15d-384d-0a4b-97ec | 10.2 | 4.0 | 6.0 | 575.0 | +---------------+----------------+---------------------+-------+-------+--------+--------+
- Parameters:
headerNames
- header name- Returns:
AbstractExcelWriter
-
disableRolling
Disables rolling excess rows.If this is invoked, excel file has only one sheet.
- Returns:
AbstractExcelWriter
-
headerStyle
-
headerStyles
public AbstractExcelWriter<W,T> headerStyles(com.github.javaxcel.styler.ExcelStyleConfig... configs) -
bodyStyle
-
bodyStyles
public AbstractExcelWriter<W,T> bodyStyles(com.github.javaxcel.styler.ExcelStyleConfig... configs) -
autoResizeCols
-
hideExtraRows
-
hideExtraCols
-
write
Writes the data in the excel file.- Specified by:
write
in interfaceExcelWriter<W extends org.apache.poi.ss.usermodel.Workbook>
- Parameters:
out
- output stream for writing excel filelist
- list of models- Throws:
IllegalArgumentException
- if list is nullIllegalArgumentException
- ifwrite(OutputStream, List)
is invoked with empty list
-
beforeWrite
-
afterWrite
-
ifHeaderNamesAreEmpty
Handles header names, if they are empty.You have to implement how to do, if the header names are empty. For examples, you can throw exception or set default header names. This process will be executed before
ExcelWriter
writes header.- Parameters:
headerNames
- header names
-
writeToSheet
Writes list of models to the sheet.- Parameters:
sheet
- sheetlist
- list of models
-
getNumOfColumns
protected abstract int getNumOfColumns()Returns the number of columns.- Returns:
- the number of columns
-