Document Solutions for Excel, .NET Edition | Document Solutions
Features / Worksheet / Range Operations / Set Custom Objects to a Range
In This Topic
    Set Custom Objects to a Range
    In This Topic

    DsExcel allows you to set custom objects or their 1D and 2D arrays to a range by using Value property 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.

    Copy Code
    //create a new workbook
    var workbook = new GrapeCity.Documents.Excel.Workbook();
    var activeSheet = workbook.ActiveSheet;
    IRange a1 = activeSheet.Range["A1"];
    var dict = new Dictionary<string, object>()
        {"TempData1", 1},
        {"TempData2", "Temp value 2"},
        {"TempData3", 3},
        {"TempData4", "Temp value 4"}
    // Set temporary data to a range
    a1.Value = dict;
    // Display the custom object later
    var obj = (IReadOnlyDictionary<string, object>)a1.Value;
    var row = 1;
    foreach (var kv in obj)
        activeSheet.Range["B" + row].Value = kv.Key;
        activeSheet.Range["C" + row].Value = kv.Value;
        row += 1;
    // Arrange
    activeSheet.Columns[0].Hidden = true;
    //save to a pdf file

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

    Copy Code
     // The JSON converter class
    class JsonNetConverter<T> : IJsonSerializer
        public static JsonNetConverter<T> Instance { get; } = new JsonNetConverter<T>();
        public object Deserialize(string json)
            var jobj = JObject.Parse(json);
            if (jobj.TryGetValue("typeName", out JToken jtok) &&
                jtok is JValue jval &&
                jval.Type == JTokenType.String &&
                (string)jval.Value == typeof(T).Name)
                return JsonConvert.DeserializeObject<T>(json);
            } // End If
            return null;
        public string Serialize(object value)
            var jObj = JObject.FromObject(value);
            jObj.Add("typeName", new JValue(typeof(T).Name));
            return jObj.ToString(Newtonsoft.Json.Formatting.None);
    } // End Class ' JsonNetConverter
    public void overrideJSON()
        // Usage
        Workbook.ValueJsonSerializer = JsonNetConverter<ValueWithUnit>.Instance;