Class AbstractExcelWriter<W extends org.apache.poi.ss.usermodel.Workbook,T>
- All Implemented Interfaces:
ExcelWriter<W,T>
- Direct Known Subclasses:
MapWriter,ModelWriter
public abstract class AbstractExcelWriter<W extends org.apache.poi.ss.usermodel.Workbook,T> extends Object implements ExcelWriter<W,T>
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[]bodyStylesprotected booleanfilteredprotected List<String>headerNamesHeader's names.protected org.apache.poi.ss.usermodel.CellStyle[]headerStylesprotected booleanrotatedWhen the number of rows to be written is greater than maximum rows of sheet, whether to write excess rows to the next sheet.protected StringsheetNamePrefix of all the sheets' names.protected booleanwillAutoResizeprotected booleanwillHideColumnsprotected booleanwillHideRowsprotected WworkbookApache POI workbook. -
Constructor Summary
Constructors Modifier Constructor Description protectedAbstractExcelWriter(W workbook) -
Method Summary
Modifier and Type Method Description protected voidafterWrite(OutputStream out, List<T> list)AbstractExcelWriter<W,T>autoResizeColumns()protected voidbeforeWrite(OutputStream out, List<T> list)AbstractExcelWriter<W,T>bodyStyle(com.github.javaxcel.styler.ExcelStyleConfig config)Sets style to body.AbstractExcelWriter<W,T>bodyStyles(com.github.javaxcel.styler.ExcelStyleConfig... configs)Sets styles to body.AbstractExcelWriter<W,T>defaultValue(String defaultValue)Sets default value when value to be written is null or empty.AbstractExcelWriter<W,T>filter()Filters all columns.protected abstract intgetNumOfColumns()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)Sets style to header.AbstractExcelWriter<W,T>headerStyles(com.github.javaxcel.styler.ExcelStyleConfig... configs)Sets styles to header.AbstractExcelWriter<W,T>hideExtraColumns()AbstractExcelWriter<W,T>hideExtraRows()protected abstract voidifHeaderNamesAreEmpty(List<String> headerNames)Handles header names, if they are empty.AbstractExcelWriter<W,T>sheetName(String sheetName)Sets sheet name.AbstractExcelWriter<W,T>unrotate()Disables to rotate sheet.voidwrite(OutputStream out, List<T> list)Writes the data in the excel file.protected abstract voidwriteToSheet(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)
-
rotated
protected boolean rotatedWhen 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:
unrotate()
-
filtered
protected boolean filtered- See Also:
filter()
-
headerStyles
protected org.apache.poi.ss.usermodel.CellStyle[] headerStyles -
bodyStyles
protected org.apache.poi.ss.usermodel.CellStyle[] bodyStyles -
willAutoResize
protected boolean willAutoResize- See Also:
autoResizeColumns()
-
willHideRows
protected boolean willHideRows- See Also:
hideExtraRows()
-
willHideColumns
protected boolean willHideColumns- See Also:
hideExtraColumns()
-
-
Constructor Details
-
Method Details
-
defaultValue
Sets default value when value to be written is null or empty.- Specified by:
defaultValuein interfaceExcelWriter<W extends org.apache.poi.ss.usermodel.Workbook,T>- 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
unrotate(), the sheet name has no suffix. You can see the sheet name like this SHEET- Specified by:
sheetNamein interfaceExcelWriter<W extends org.apache.poi.ss.usermodel.Workbook,T>- Parameters:
sheetName- sheet name- Returns:
AbstractExcelWriter- Throws:
IllegalArgumentException- if sheet name is invalid- See Also:
unrotate(),WorkbookUtil.validateSheetName(String)
-
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 | +---------------+----------------+---------------------+-------+-------+--------+--------+- Specified by:
headerNamesin interfaceExcelWriter<W extends org.apache.poi.ss.usermodel.Workbook,T>- Parameters:
headerNames- header names- Returns:
AbstractExcelWriter- See Also:
createHeader(Sheet)
-
unrotate
Disables to rotate sheet.If this is invoked, excel file has only one sheet.
- Specified by:
unrotatein interfaceExcelWriter<W extends org.apache.poi.ss.usermodel.Workbook,T>- Returns:
AbstractExcelWriter
-
filter
Filters all columns.- Specified by:
filterin interfaceExcelWriter<W extends org.apache.poi.ss.usermodel.Workbook,T>- Returns:
AbstractExcelWriter
-
headerStyle
Sets style to header.- Specified by:
headerStylein interfaceExcelWriter<W extends org.apache.poi.ss.usermodel.Workbook,T>- Parameters:
config- style config- Returns:
AbstractExcelWriter
-
headerStyles
public AbstractExcelWriter<W,T> headerStyles(com.github.javaxcel.styler.ExcelStyleConfig... configs)Sets styles to header.- Specified by:
headerStylesin interfaceExcelWriter<W extends org.apache.poi.ss.usermodel.Workbook,T>- Parameters:
configs- style configs- Returns:
AbstractExcelWriter
-
bodyStyle
Sets style to body.- Specified by:
bodyStylein interfaceExcelWriter<W extends org.apache.poi.ss.usermodel.Workbook,T>- Parameters:
config- style config- Returns:
AbstractExcelWriter
-
bodyStyles
public AbstractExcelWriter<W,T> bodyStyles(com.github.javaxcel.styler.ExcelStyleConfig... configs)Sets styles to body.- Specified by:
bodyStylesin interfaceExcelWriter<W extends org.apache.poi.ss.usermodel.Workbook,T>- Parameters:
configs- style configs- Returns:
AbstractExcelWriter
-
autoResizeColumns
- Specified by:
autoResizeColumnsin interfaceExcelWriter<W extends org.apache.poi.ss.usermodel.Workbook,T>- Returns:
AbstractExcelWriter- See Also:
write(OutputStream, List)
-
hideExtraRows
This will automatically create rows up to the maximum number of rows and hide them. This action takes more time and makes file size bigger than it doesn't.
- Specified by:
hideExtraRowsin interfaceExcelWriter<W extends org.apache.poi.ss.usermodel.Workbook,T>- Returns:
AbstractExcelWriter- See Also:
write(OutputStream, List)
-
hideExtraColumns
- Specified by:
hideExtraColumnsin interfaceExcelWriter<W extends org.apache.poi.ss.usermodel.Workbook,T>- Returns:
AbstractExcelWriter- See Also:
write(OutputStream, List)
-
write
Writes the data in the excel file.- Specified by:
writein interfaceExcelWriter<W extends org.apache.poi.ss.usermodel.Workbook,T>- 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
ExcelWriterwrites 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
-