Class MapWriter<W extends org.apache.poi.ss.usermodel.Workbook,T extends Map<String,?>>
- Type Parameters:
W
- excel workbookT
-Map
- All Implemented Interfaces:
ExcelWriter<T>
public final class MapWriter<W extends org.apache.poi.ss.usermodel.Workbook,T extends Map<String,?>> extends AbstractExcelWriter<W,T>
Map
.-
Field Summary
Fields inherited from class com.github.javaxcel.out.AbstractExcelWriter
bodyStyles, headerNames, headerStyles, rolling, sheetName, willAutoResize, willHideCols, willHideRows, workbook
-
Method Summary
Modifier and Type Method Description MapWriter<W,T>
autoResizeCols()
protected void
beforeWrite(OutputStream out, List<T> list)
MapWriter<W,T>
bodyStyle(com.github.javaxcel.styler.ExcelStyleConfig config)
MapWriter<W,T>
bodyStyles(com.github.javaxcel.styler.ExcelStyleConfig... configs)
MapWriter<W,T>
defaultValue(String defaultValue)
Sets default value when value to be written is null or empty.MapWriter<W,T>
disableRolling()
Disables rolling excess rows.protected int
getNumOfColumns()
Returns the number of columns.MapWriter<W,T>
headerNames(List<String> orderedKeys)
Rearranges the keys ofMap
with custom order.MapWriter<W,T>
headerNames(List<String> orderedKeys, List<String> headerNames)
Rearranges the keys ofMap
with custom order and sets header names.MapWriter<W,T>
headerStyle(com.github.javaxcel.styler.ExcelStyleConfig config)
MapWriter<W,T>
headerStyles(com.github.javaxcel.styler.ExcelStyleConfig... configs)
MapWriter<W,T>
hideExtraCols()
MapWriter<W,T>
hideExtraRows()
protected void
ifHeaderNamesAreEmpty(List<String> headerNames)
Handles header names, if they are empty.MapWriter<W,T>
sheetName(String sheetName)
Sets sheet name.protected void
writeToSheet(org.apache.poi.ss.usermodel.Sheet sheet, List<T> list)
Writes list of models to the sheet.
-
Method Details
-
defaultValue
Sets default value when value to be written is null or empty.- Overrides:
defaultValue
in classAbstractExcelWriter<W extends org.apache.poi.ss.usermodel.Workbook,T extends Map<String,?>>
- Parameters:
defaultValue
- replacement of the value when it is null or empty string.- Returns:
MapWriter
-
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.disableRolling()
, the sheet name has no suffix. You can see the sheet name like this SHEET- Overrides:
sheetName
in classAbstractExcelWriter<W extends org.apache.poi.ss.usermodel.Workbook,T extends Map<String,?>>
- Parameters:
sheetName
- sheet name- Returns:
MapWriter
- See Also:
AbstractExcelWriter.disableRolling()
-
disableRolling
Disables rolling excess rows.If this is invoked, excel file has only one sheet.
- Overrides:
disableRolling
in classAbstractExcelWriter<W extends org.apache.poi.ss.usermodel.Workbook,T extends Map<String,?>>
- Returns:
MapWriter
-
headerNames
Rearranges the keys ofMap
with custom order.If you export list of
Map
as a excel file, column order is not guaranteed, unless the type of its instance isLinkedHashMap
. 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 rearrange the column order, place the keys in the order you want like this.
List<String> orderedKeys = Arrays.asList( "width" "depth", "height", "weight", "serialNumber", "name", "apiId"); ExcelWriterFactory.create(new SXSSFWorkbook()) .headerNames(orderedKeys) .write(new FileOutputStream(file), list);
Then the columns will be arranged in the order you want.
+-------+-------+--------+--------+--------------+----------------+---------------------+ | width | depth | height | weight | serialNumber | name | apiId | +-------+-------+--------+--------+--------------+----------------+---------------------+ | | 0.0 | 20.5 | 580.5 | 10000 | Choco cereal | 2a60-4973-aec0-685e | +-------+-------+--------+--------+--------------+----------------+---------------------+ | 10.2 | 4.0 | 6.0 | 575.0 | 10001 | Oatmeal cereal | f15d-384d-0a4b-97ec | +-------+-------+--------+--------+--------------+----------------+---------------------+
- Overrides:
headerNames
in classAbstractExcelWriter<W extends org.apache.poi.ss.usermodel.Workbook,T extends Map<String,?>>
- Parameters:
orderedKeys
- keys ordered as you want- Returns:
MapWriter
- Throws:
IllegalArgumentException
- if ordered keys is null or empty
-
headerNames
Rearranges the keys ofMap
with custom order and sets header names.If you export list of
Map
as a excel file, column order is not guaranteed, unless the type of its instance isLinkedHashMap
. 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 rearrange the column order, place the keys in the order you want to the first argument. To change the header names, place the names you want them changed to in the custom order to the second argument like this.
List<String> orderedKeys = Arrays.asList( "width" "depth", "height", "weight", "serialNumber", "name", "apiId"); List<String> name = Arrays.asList( "WIDTH" "DEPTH", "HEIGHT", "WEIGHT", "SERIAL_NUMBER", "NAME", "API_ID"); ExcelWriterFactory.create(new SXSSFWorkbook()) .headerNames(orderedKeys, name) .write(new FileOutputStream(file), list);
Then the column order and the names will be changed you want.
+-------+-------+--------+--------+---------------+----------------+---------------------+ | WIDTH | DEPTH | HEIGHT | WEIGHT | SERIAL_NUMBER | NAME | API_ID | +-------+-------+--------+--------+---------------+----------------+---------------------+ | | 0.0 | 20.5 | 580.5 | 10000 | Choco cereal | 2a60-4973-aec0-685e | +-------+-------+--------+--------+---------------+----------------+---------------------+ | 10.2 | 4.0 | 6.0 | 575.0 | 10001 | Oatmeal cereal | f15d-384d-0a4b-97ec | +-------+-------+--------+--------+---------------+----------------+---------------------+
- Parameters:
orderedKeys
- keys ordered as you wantheaderNames
- header names in key order- Returns:
MapWriter
- Throws:
IllegalArgumentException
- if ordered keys is null or emptyIllegalArgumentException
- if num of ordered keys is not equal to num of header names
-
headerStyle
- Overrides:
headerStyle
in classAbstractExcelWriter<W extends org.apache.poi.ss.usermodel.Workbook,T extends Map<String,?>>
- Returns:
MapWriter
-
headerStyles
- Overrides:
headerStyles
in classAbstractExcelWriter<W extends org.apache.poi.ss.usermodel.Workbook,T extends Map<String,?>>
- Returns:
MapWriter
-
bodyStyle
-
bodyStyles
- Overrides:
bodyStyles
in classAbstractExcelWriter<W extends org.apache.poi.ss.usermodel.Workbook,T extends Map<String,?>>
- Returns:
MapWriter
-
autoResizeCols
- Overrides:
autoResizeCols
in classAbstractExcelWriter<W extends org.apache.poi.ss.usermodel.Workbook,T extends Map<String,?>>
- Returns:
MapWriter
-
hideExtraRows
- Overrides:
hideExtraRows
in classAbstractExcelWriter<W extends org.apache.poi.ss.usermodel.Workbook,T extends Map<String,?>>
- Returns:
MapWriter
-
hideExtraCols
- Overrides:
hideExtraCols
in classAbstractExcelWriter<W extends org.apache.poi.ss.usermodel.Workbook,T extends Map<String,?>>
- Returns:
MapWriter
-
beforeWrite
Unlike
ModelWriter
,MapWriter
cannot validate header names, header styles and body styles beforeAbstractExcelWriter.write(OutputStream, List)
is invoked because the only way to getMap
exists in that method. So it does using this hook.- Overrides:
beforeWrite
in classAbstractExcelWriter<W extends org.apache.poi.ss.usermodel.Workbook,T extends Map<String,?>>
-
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.If the header names are not set through
headerNames(List, List)
, this method brings the values fromMap.keySet()
.- Specified by:
ifHeaderNamesAreEmpty
in classAbstractExcelWriter<W extends org.apache.poi.ss.usermodel.Workbook,T extends Map<String,?>>
- Parameters:
headerNames
- header names- See Also:
Map.keySet()
-
writeToSheet
Writes list of models to the sheet.- Specified by:
writeToSheet
in classAbstractExcelWriter<W extends org.apache.poi.ss.usermodel.Workbook,T extends Map<String,?>>
- Parameters:
sheet
- sheetlist
- list of models
-
getNumOfColumns
protected int getNumOfColumns()Returns the number of columns.- Specified by:
getNumOfColumns
in classAbstractExcelWriter<W extends org.apache.poi.ss.usermodel.Workbook,T extends Map<String,?>>
- Returns:
- the number of columns
-