Class ModelWriter<W extends org.apache.poi.ss.usermodel.Workbook,T>
- Type Parameters:
W- excel workbookT- type of model
- All Implemented Interfaces:
ExcelWriter<W,T>
public class ModelWriter<W extends org.apache.poi.ss.usermodel.Workbook,T> extends AbstractExcelWriter<W,T>
-
Field Summary
Fields inherited from class com.github.javaxcel.out.AbstractExcelWriter
bodyStyles, filtered, headerNames, headerStyles, rotated, sheetName, willAutoResize, willHideColumns, willHideRows, workbook -
Constructor Summary
Constructors Constructor Description ModelWriter(W workbook, Class<T> type) -
Method Summary
Modifier and Type Method Description ModelWriter<W,T>autoResizeColumns()protected voidbeforeWrite(OutputStream out, List<T> list)ModelWriter<W,T>bodyStyle(com.github.javaxcel.styler.ExcelStyleConfig config)Sets style to body.ModelWriter<W,T>bodyStyles(com.github.javaxcel.styler.ExcelStyleConfig... configs)Sets styles to body.ModelWriter<W,T>defaultValue(String defaultValue)Sets default value when value to be written is null or empty.ModelWriter<W,T>enumDropdown()Enables to create dropdowns for columns of enum.ModelWriter<W,T>filter()Filters all columns.protected intgetNumOfColumns()Returns the number of columns.ModelWriter<W,T>headerNames(List<String> headerNames)Sets header names.ModelWriter<W,T>headerStyle(com.github.javaxcel.styler.ExcelStyleConfig config)Sets style to header.ModelWriter<W,T>headerStyles(com.github.javaxcel.styler.ExcelStyleConfig... configs)Sets styles to header.ModelWriter<W,T>hideExtraColumns()ModelWriter<W,T>hideExtraRows()protected voidifHeaderNamesAreEmpty(List<String> headerNames)Handles header names, if they are empty.ModelWriter<W,T>sheetName(String sheetName)Sets sheet name.ModelWriter<W,T>unrotate()Disables to rotate sheet.protected voidwriteToSheet(org.apache.poi.ss.usermodel.Sheet sheet, List<T> list)Writes list of models to the sheet.
-
Constructor Details
-
ModelWriter
- Parameters:
workbook- excel workbooktype- type of model- See Also:
ExcelWriterFactory.create(Workbook, Class)
-
-
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>- Overrides:
defaultValuein classAbstractExcelWriter<W extends org.apache.poi.ss.usermodel.Workbook,T>- Parameters:
defaultValue- replacement of the value when it is null or empty string.- Returns:
ModelWriter
-
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
AbstractExcelWriter.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>- Overrides:
sheetNamein classAbstractExcelWriter<W extends org.apache.poi.ss.usermodel.Workbook,T>- Parameters:
sheetName- sheet name- Returns:
ModelWriter- See Also:
AbstractExcelWriter.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>- Overrides:
headerNamesin classAbstractExcelWriter<W extends org.apache.poi.ss.usermodel.Workbook,T>- Parameters:
headerNames- header names- Returns:
ModelWriter- See Also:
AbstractExcelWriter.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>- Overrides:
unrotatein classAbstractExcelWriter<W extends org.apache.poi.ss.usermodel.Workbook,T>- Returns:
ModelWriter
-
filter
Filters all columns.- Specified by:
filterin interfaceExcelWriter<W extends org.apache.poi.ss.usermodel.Workbook,T>- Overrides:
filterin classAbstractExcelWriter<W extends org.apache.poi.ss.usermodel.Workbook,T>- Returns:
ModelWriter
-
enumDropdown
Enables to create dropdowns for columns of enum.If this is invoked, excel file has only one sheet.
- Returns:
ModelWriter- See Also:
ExcelModel.enumDropdown(),ExcelColumn.enumDropdown()
-
headerStyle
Sets style to header.- Specified by:
headerStylein interfaceExcelWriter<W extends org.apache.poi.ss.usermodel.Workbook,T>- Overrides:
headerStylein classAbstractExcelWriter<W extends org.apache.poi.ss.usermodel.Workbook,T>- Parameters:
config- style config- Returns:
ModelWriter
-
headerStyles
Sets styles to header.- Specified by:
headerStylesin interfaceExcelWriter<W extends org.apache.poi.ss.usermodel.Workbook,T>- Overrides:
headerStylesin classAbstractExcelWriter<W extends org.apache.poi.ss.usermodel.Workbook,T>- Parameters:
configs- style configs- Returns:
ModelWriter
-
bodyStyle
Sets style to body.- Specified by:
bodyStylein interfaceExcelWriter<W extends org.apache.poi.ss.usermodel.Workbook,T>- Overrides:
bodyStylein classAbstractExcelWriter<W extends org.apache.poi.ss.usermodel.Workbook,T>- Parameters:
config- style config- Returns:
ModelWriter
-
bodyStyles
Sets styles to body.- Specified by:
bodyStylesin interfaceExcelWriter<W extends org.apache.poi.ss.usermodel.Workbook,T>- Overrides:
bodyStylesin classAbstractExcelWriter<W extends org.apache.poi.ss.usermodel.Workbook,T>- Parameters:
configs- style configs- Returns:
ModelWriter
-
autoResizeColumns
- Specified by:
autoResizeColumnsin interfaceExcelWriter<W extends org.apache.poi.ss.usermodel.Workbook,T>- Overrides:
autoResizeColumnsin classAbstractExcelWriter<W extends org.apache.poi.ss.usermodel.Workbook,T>- Returns:
ModelWriter- See Also:
AbstractExcelWriter.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>- Overrides:
hideExtraRowsin classAbstractExcelWriter<W extends org.apache.poi.ss.usermodel.Workbook,T>- Returns:
ModelWriter- See Also:
AbstractExcelWriter.write(OutputStream, List)
-
hideExtraColumns
- Specified by:
hideExtraColumnsin interfaceExcelWriter<W extends org.apache.poi.ss.usermodel.Workbook,T>- Overrides:
hideExtraColumnsin classAbstractExcelWriter<W extends org.apache.poi.ss.usermodel.Workbook,T>- Returns:
ModelWriter- See Also:
AbstractExcelWriter.write(OutputStream, List)
-
beforeWrite
- Overrides:
beforeWritein classAbstractExcelWriter<W extends org.apache.poi.ss.usermodel.Workbook,T>- See Also:
createDropdowns(Sheet)
-
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.If the header names are not set through
headerNames(List), this method brings the values fromExcelColumn.name().- Specified by:
ifHeaderNamesAreEmptyin classAbstractExcelWriter<W extends org.apache.poi.ss.usermodel.Workbook,T>- Parameters:
headerNames- header names- See Also:
FieldUtils.toHeaderNames(List)
-
writeToSheet
Writes list of models to the sheet.- Specified by:
writeToSheetin classAbstractExcelWriter<W extends org.apache.poi.ss.usermodel.Workbook,T>- Parameters:
sheet- sheetlist- list of models
-
getNumOfColumns
protected int getNumOfColumns()Returns the number of columns.- Specified by:
getNumOfColumnsin classAbstractExcelWriter<W extends org.apache.poi.ss.usermodel.Workbook,T>- Returns:
- the number of columns
-