Class MapWriter<W extends org.apache.poi.ss.usermodel.Workbook,T extends Map<String,?>>
- Type Parameters:
W- excel workbookT-Map
- All Implemented Interfaces:
ExcelWriter<W,T>
public 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, filtered, headerNames, headerStyles, rotated, sheetName, willAutoResize, willHideColumns, willHideRows, workbook -
Constructor Summary
-
Method Summary
Modifier and Type Method Description MapWriter<W,T>autoResizeColumns()protected voidbeforeWrite(OutputStream out, List<T> list)MapWriter<W,T>bodyStyle(com.github.javaxcel.styler.ExcelStyleConfig config)Sets style to body.MapWriter<W,T>bodyStyles(com.github.javaxcel.styler.ExcelStyleConfig... configs)Sets styles to body.MapWriter<W,T>defaultValue(String defaultValue)Sets default value when value to be written is null or empty.MapWriter<W,T>filter()Filters all columns.protected intgetNumOfColumns()Returns the number of columns.MapWriter<W,T>headerNames(List<String> orderedKeys)Rearranges the keys ofMapwith custom order.MapWriter<W,T>headerNames(List<String> orderedKeys, List<String> headerNames)Rearranges the keys ofMapwith custom order and sets header names.MapWriter<W,T>headerStyle(com.github.javaxcel.styler.ExcelStyleConfig config)Sets style to header.MapWriter<W,T>headerStyles(com.github.javaxcel.styler.ExcelStyleConfig... configs)Sets styles to header.MapWriter<W,T>hideExtraColumns()MapWriter<W,T>hideExtraRows()protected voidifHeaderNamesAreEmpty(List<String> headerNames)Handles header names, if they are empty.MapWriter<W,T>sheetName(String sheetName)Sets sheet name.MapWriter<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
-
MapWriter
- Parameters:
workbook- excel workbook- See Also:
ExcelWriterFactory.create(Workbook)
-
-
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 extends Map<String,?>>- Overrides:
defaultValuein 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.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 extends Map<String,?>>- Overrides:
sheetNamein classAbstractExcelWriter<W extends org.apache.poi.ss.usermodel.Workbook,T extends Map<String,?>>- Parameters:
sheetName- sheet name- Returns:
MapWriter- See Also:
AbstractExcelWriter.unrotate(),WorkbookUtil.validateSheetName(String)
-
headerNames
Rearranges the keys ofMapwith custom order.If you export list of
Mapas 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 | +-------+-------+--------+--------+--------------+----------------+---------------------+- Specified by:
headerNamesin interfaceExcelWriter<W extends org.apache.poi.ss.usermodel.Workbook,T extends Map<String,?>>- Overrides:
headerNamesin 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- See Also:
AbstractExcelWriter.createHeader(Sheet)
-
headerNames
Rearranges the keys ofMapwith custom order and sets header names.If you export list of
Mapas 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
-
unrotate
Disables to rotate sheet.If this is invoked, excel file has only one sheet.
-
filter
Filters all columns. -
headerStyle
Sets style to header.- Specified by:
headerStylein interfaceExcelWriter<W extends org.apache.poi.ss.usermodel.Workbook,T extends Map<String,?>>- Overrides:
headerStylein classAbstractExcelWriter<W extends org.apache.poi.ss.usermodel.Workbook,T extends Map<String,?>>- Parameters:
config- style config- Returns:
MapWriter
-
headerStyles
Sets styles to header.- Specified by:
headerStylesin interfaceExcelWriter<W extends org.apache.poi.ss.usermodel.Workbook,T extends Map<String,?>>- Overrides:
headerStylesin classAbstractExcelWriter<W extends org.apache.poi.ss.usermodel.Workbook,T extends Map<String,?>>- Parameters:
configs- style configs- Returns:
MapWriter
-
bodyStyle
Sets style to body. -
bodyStyles
Sets styles to body.- Specified by:
bodyStylesin interfaceExcelWriter<W extends org.apache.poi.ss.usermodel.Workbook,T extends Map<String,?>>- Overrides:
bodyStylesin classAbstractExcelWriter<W extends org.apache.poi.ss.usermodel.Workbook,T extends Map<String,?>>- Parameters:
configs- style configs- Returns:
MapWriter
-
autoResizeColumns
- Specified by:
autoResizeColumnsin interfaceExcelWriter<W extends org.apache.poi.ss.usermodel.Workbook,T extends Map<String,?>>- Overrides:
autoResizeColumnsin classAbstractExcelWriter<W extends org.apache.poi.ss.usermodel.Workbook,T extends Map<String,?>>- Returns:
MapWriter- 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 extends Map<String,?>>- Overrides:
hideExtraRowsin classAbstractExcelWriter<W extends org.apache.poi.ss.usermodel.Workbook,T extends Map<String,?>>- Returns:
MapWriter- See Also:
AbstractExcelWriter.write(OutputStream, List)
-
hideExtraColumns
- Specified by:
hideExtraColumnsin interfaceExcelWriter<W extends org.apache.poi.ss.usermodel.Workbook,T extends Map<String,?>>- Overrides:
hideExtraColumnsin classAbstractExcelWriter<W extends org.apache.poi.ss.usermodel.Workbook,T extends Map<String,?>>- Returns:
MapWriter- See Also:
AbstractExcelWriter.write(OutputStream, List)
-
beforeWrite
Unlike
ModelWriter,MapWritercannot validate header names, header styles and body styles beforeAbstractExcelWriter.write(OutputStream, List)is invoked because the only way to getMapexists in that method. So it does using this hook.- Overrides:
beforeWritein 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
ExcelWriterwrites header.If the header names are not set through
headerNames(List, List), this method brings the values fromMap.keySet().- Specified by:
ifHeaderNamesAreEmptyin 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:
writeToSheetin 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:
getNumOfColumnsin classAbstractExcelWriter<W extends org.apache.poi.ss.usermodel.Workbook,T extends Map<String,?>>- Returns:
- the number of columns
-