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<T>

public final class MapWriter<W extends org.apache.poi.ss.usermodel.Workbook,​T extends Map<String,​?>>
extends AbstractExcelWriter<W,​T>
Excel writer for Map.
  • Method Details

    • defaultValue

      public MapWriter<W,​T> defaultValue​(String defaultValue)
      Sets default value when value to be written is null or empty.
      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.disableRolling(), the sheet name has no suffix. You can see the sheet name like this SHEET

      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.disableRolling()
    • disableRolling

      public MapWriter<W,​T> disableRolling()
      Disables rolling excess rows.

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

      Overrides:
      disableRolling in class AbstractExcelWriter<W extends org.apache.poi.ss.usermodel.Workbook,​T extends Map<String,​?>>
      Returns:
      MapWriter
    • 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 |
       +-------+-------+--------+--------+--------------+----------------+---------------------+
       
      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
    • 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
    • headerStyle

      public MapWriter<W,​T> headerStyle​(com.github.javaxcel.styler.ExcelStyleConfig config)
      Overrides:
      headerStyle in class AbstractExcelWriter<W extends org.apache.poi.ss.usermodel.Workbook,​T extends Map<String,​?>>
      Returns:
      MapWriter
    • headerStyles

      public MapWriter<W,​T> headerStyles​(com.github.javaxcel.styler.ExcelStyleConfig... configs)
      Overrides:
      headerStyles in class AbstractExcelWriter<W extends org.apache.poi.ss.usermodel.Workbook,​T extends Map<String,​?>>
      Returns:
      MapWriter
    • bodyStyle

      public MapWriter<W,​T> bodyStyle​(com.github.javaxcel.styler.ExcelStyleConfig config)
      Overrides:
      bodyStyle in class AbstractExcelWriter<W extends org.apache.poi.ss.usermodel.Workbook,​T extends Map<String,​?>>
      Returns:
      MapWriter
    • bodyStyles

      public MapWriter<W,​T> bodyStyles​(com.github.javaxcel.styler.ExcelStyleConfig... configs)
      Overrides:
      bodyStyles in class AbstractExcelWriter<W extends org.apache.poi.ss.usermodel.Workbook,​T extends Map<String,​?>>
      Returns:
      MapWriter
    • autoResizeCols

      public MapWriter<W,​T> autoResizeCols()
      Overrides:
      autoResizeCols in class AbstractExcelWriter<W extends org.apache.poi.ss.usermodel.Workbook,​T extends Map<String,​?>>
      Returns:
      MapWriter
    • hideExtraRows

      public MapWriter<W,​T> hideExtraRows()
      Overrides:
      hideExtraRows in class AbstractExcelWriter<W extends org.apache.poi.ss.usermodel.Workbook,​T extends Map<String,​?>>
      Returns:
      MapWriter
    • hideExtraCols

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