Documents for Excel, Java Edition Documentation
Features / Worksheet / Range Operations / Set Custom Objects to a Range
In This Topic
    Set Custom Objects to a Range
    In This Topic

    GcExcel allows you to set custom objects or their 1D and 2D arrays to a range by using setValue method of IRange interface. Custom objects are not supported for Excel I/O though.

    Behavior of Custom Objects with Below Operations


    Refer to the following example code to set 2D array of custom objects to a range.

    Java
    Copy Code
    //create a new workbook
    Workbook workbook = new Workbook();
    IWorksheet activeSheet = workbook.getActiveSheet();
    IRange a1 = activeSheet.getRange("A1");
    HashMap<String, Object> dict = new HashMap<String, Object>();
    dict.put("TempData1", 1);
    dict.put("TempData2", "Temp value 2");
    dict.put("TempData3", 3);
    dict.put("TempData4", "Temp value 4");
    
    // Set temporary data to a range
    a1.setValue(dict);
    
    // Display the custom object later
    HashMap<String, Object> obj = (HashMap<String, Object>)a1.getValue();
    int row = 1;
    for (Map.Entry<String, Object> kv : obj.entrySet())
    {
    activeSheet.getRange("B" + row).setValue(kv.getKey());
    activeSheet.getRange("C" + row).setValue(kv.getValue());
    row += 1;
    }
    
    // Arrange
    activeSheet.getColumns().autoFit();
    activeSheet.getColumns().get(0).setHidden(true);
    
    //save to an pdf file
    workbook.save("SetCustomRangeValue.pdf");
    

    Refer to the following example code to override JSON serialization behavior.

    Java
    Copy Code
    // The JSON converter class
    class GsonConverter<T> implements IJsonSerializer {
        private final Gson _gson = new Gson();
        private final Class<T> _type;
     
        public GsonConverter(Class<T> type) {
            _type = type;
        }
     
        private final GenericStaticFieldValueProvider<GsonConverter<?>> s_instanceProvider =
            new GenericStaticFieldValueProvider<GsonConverter<?>>();
     
        public <T> GsonConverter<T> GetInstance(Class<T> canon) {
            GsonConverter<T> instance = (GsonConverter<T>) s_instanceProvider.getValue(canon);
            if (instance != null) {
                return instance;
            }
            instance = new GsonConverter<T>(canon);
            s_instanceProvider.setValue(canon, instance);
            return instance;
        }
     
        public final Object deserialize(String json) {
            JsonObject jObject = new JsonParser().parse(json).getAsJsonObject();
            String typeName = jObject.get("typeName").getAsString();
            if (typeName.equals(_type.getSimpleName())) {
                jObject.remove("typeName");
                return _gson.fromJson(jObject, _type);
            }
            return null;
        }
     
        public final String serialize(Object value) {
            JsonObject jObject = _gson.toJsonTree(value).getAsJsonObject();
            jObject.addProperty("typeName", _type.getSimpleName());
            return _gson.toJson(jObject);
        }
     
    } // End Class ' GsonConverter
     
    // Workaround for "Cannot make a static reference to the non-static type T"
    class GenericStaticFieldValueProvider<TValue> {
        private final ConcurrentHashMap<Class<?>, TValue> _value = new ConcurrentHashMap<Class<?>, TValue>();
        public TValue getValue(Class<?> canon) {
            return (TValue) _value.get(canon);
        }
        public void setValue(Class<?> canon, TValue value) {
            _value.put(canon, value);
        }
    } // End Class
        
    public void overrideJSON() {
        // Usage
        Workbook.setValueJsonSerializer(GsonConverter.GetInstance(ValueWithUnit.class));
    }
    

    Please Note:

    • In JSR310 Java version, the date and time types are treated as custom objects if the extension package is not loaded.
    • java.math.BigInteger and java.math.BigDecimal are treated as custom objects instead of java.lang.Double.