Class MapWriter<W extends org.apache.poi.ss.usermodel.Workbook,​T extends Map<String,​?>>

java.lang.Object
com.github.javaxcel.out.AbstractExcelWriter<W,​T>
com.github.javaxcel.out.MapWriter<W,​T>
Type Parameters:
W - excel workbook
T - 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>
Excel writer for Map.
  • Constructor Details

  • Method Details

    • defaultValue

      public MapWriter<W,​T> defaultValue​(String defaultValue)
      Sets default value when value to be written is null or empty.
      Specified by:
      defaultValue in interface ExcelWriter<W extends org.apache.poi.ss.usermodel.Workbook,​T extends Map<String,​?>>
      Overrides:
      defaultValue in class AbstractExcelWriter<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

      public MapWriter<W,​T> sheetName​(String 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:
      sheetName in interface ExcelWriter<W extends org.apache.poi.ss.usermodel.Workbook,​T extends Map<String,​?>>
      Overrides:
      sheetName in class AbstractExcelWriter<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

      public MapWriter<W,​T> headerNames​(List<String> orderedKeys)
      Rearranges the keys of Map with custom order.

      If you export list of Map as a excel file, column order is not guaranteed, unless the type of its instance is LinkedHashMap. 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:
      headerNames in interface ExcelWriter<W extends org.apache.poi.ss.usermodel.Workbook,​T extends Map<String,​?>>
      Overrides:
      headerNames in class AbstractExcelWriter<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

      public MapWriter<W,​T> headerNames​(List<String> orderedKeys, @Nullable List<String> headerNames)
      Rearranges the keys of Map 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 is LinkedHashMap. 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 want
      headerNames - header names in key order
      Returns:
      MapWriter
      Throws:
      IllegalArgumentException - if ordered keys is null or empty
      IllegalArgumentException - if num of ordered keys is not equal to num of header names
    • unrotate

      public MapWriter<W,​T> unrotate()
      Disables to rotate sheet.

      If this is invoked, excel file has only one sheet.

      Specified by:
      unrotate in interface ExcelWriter<W extends org.apache.poi.ss.usermodel.Workbook,​T extends Map<String,​?>>
      Overrides:
      unrotate in class AbstractExcelWriter<W extends org.apache.poi.ss.usermodel.Workbook,​T extends Map<String,​?>>
      Returns:
      MapWriter
    • filter

      public MapWriter<W,​T> filter()
      Filters all columns.
      Specified by:
      filter in interface ExcelWriter<W extends org.apache.poi.ss.usermodel.Workbook,​T extends Map<String,​?>>
      Overrides:
      filter in class AbstractExcelWriter<W extends org.apache.poi.ss.usermodel.Workbook,​T extends Map<String,​?>>
      Returns:
      MapWriter
    • headerStyle

      public MapWriter<W,​T> headerStyle​(com.github.javaxcel.styler.ExcelStyleConfig config)
      Sets style to header.
      Specified by:
      headerStyle in interface ExcelWriter<W extends org.apache.poi.ss.usermodel.Workbook,​T extends Map<String,​?>>
      Overrides:
      headerStyle in class AbstractExcelWriter<W extends org.apache.poi.ss.usermodel.Workbook,​T extends Map<String,​?>>
      Parameters:
      config - style config
      Returns:
      MapWriter
    • headerStyles

      public MapWriter<W,​T> headerStyles​(com.github.javaxcel.styler.ExcelStyleConfig... configs)
      Sets styles to header.
      Specified by:
      headerStyles in interface ExcelWriter<W extends org.apache.poi.ss.usermodel.Workbook,​T extends Map<String,​?>>
      Overrides:
      headerStyles in class AbstractExcelWriter<W extends org.apache.poi.ss.usermodel.Workbook,​T extends Map<String,​?>>
      Parameters:
      configs - style configs
      Returns:
      MapWriter
    • bodyStyle

      public MapWriter<W,​T> bodyStyle​(com.github.javaxcel.styler.ExcelStyleConfig config)
      Sets style to body.
      Specified by:
      bodyStyle in interface ExcelWriter<W extends org.apache.poi.ss.usermodel.Workbook,​T extends Map<String,​?>>
      Overrides:
      bodyStyle in class AbstractExcelWriter<W extends org.apache.poi.ss.usermodel.Workbook,​T extends Map<String,​?>>
      Parameters:
      config - style config
      Returns:
      MapWriter
    • bodyStyles

      public MapWriter<W,​T> bodyStyles​(com.github.javaxcel.styler.ExcelStyleConfig... configs)
      Sets styles to body.
      Specified by:
      bodyStyles in interface ExcelWriter<W extends org.apache.poi.ss.usermodel.Workbook,​T extends Map<String,​?>>
      Overrides:
      bodyStyles in class AbstractExcelWriter<W extends org.apache.poi.ss.usermodel.Workbook,​T extends Map<String,​?>>
      Parameters:
      configs - style configs
      Returns:
      MapWriter
    • autoResizeColumns

      public MapWriter<W,​T> autoResizeColumns()
      Specified by:
      autoResizeColumns in interface ExcelWriter<W extends org.apache.poi.ss.usermodel.Workbook,​T extends Map<String,​?>>
      Overrides:
      autoResizeColumns in class AbstractExcelWriter<W extends org.apache.poi.ss.usermodel.Workbook,​T extends Map<String,​?>>
      Returns:
      MapWriter
      See Also:
      AbstractExcelWriter.write(OutputStream, List)
    • hideExtraRows

      public MapWriter<W,​T> 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:
      hideExtraRows in interface ExcelWriter<W extends org.apache.poi.ss.usermodel.Workbook,​T extends Map<String,​?>>
      Overrides:
      hideExtraRows in class AbstractExcelWriter<W extends org.apache.poi.ss.usermodel.Workbook,​T extends Map<String,​?>>
      Returns:
      MapWriter
      See Also:
      AbstractExcelWriter.write(OutputStream, List)
    • hideExtraColumns

      public MapWriter<W,​T> hideExtraColumns()
      Specified by:
      hideExtraColumns in interface ExcelWriter<W extends org.apache.poi.ss.usermodel.Workbook,​T extends Map<String,​?>>
      Overrides:
      hideExtraColumns in class AbstractExcelWriter<W extends org.apache.poi.ss.usermodel.Workbook,​T extends Map<String,​?>>
      Returns:
      MapWriter
      See Also:
      AbstractExcelWriter.write(OutputStream, List)
    • beforeWrite

      protected void beforeWrite​(OutputStream out, List<T> list)

      Unlike ModelWriter, MapWriter cannot validate header names, header styles and body styles before AbstractExcelWriter.write(OutputStream, List) is invoked because the only way to get Map exists in that method. So it does using this hook.

      Overrides:
      beforeWrite in class AbstractExcelWriter<W extends org.apache.poi.ss.usermodel.Workbook,​T extends Map<String,​?>>
    • ifHeaderNamesAreEmpty

      protected void ifHeaderNamesAreEmpty​(List<String> headerNames)
      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 from Map.keySet().

      Specified by:
      ifHeaderNamesAreEmpty in class AbstractExcelWriter<W extends org.apache.poi.ss.usermodel.Workbook,​T extends Map<String,​?>>
      Parameters:
      headerNames - header names
      See Also:
      Map.keySet()
    • writeToSheet

      protected void writeToSheet​(org.apache.poi.ss.usermodel.Sheet sheet, List<T> list)
      Writes list of models to the sheet.
      Specified by:
      writeToSheet in class AbstractExcelWriter<W extends org.apache.poi.ss.usermodel.Workbook,​T extends Map<String,​?>>
      Parameters:
      sheet - sheet
      list - list of models
    • getNumOfColumns

      protected int getNumOfColumns()
      Returns the number of columns.
      Specified by:
      getNumOfColumns in class AbstractExcelWriter<W extends org.apache.poi.ss.usermodel.Workbook,​T extends Map<String,​?>>
      Returns:
      the number of columns