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.
While using below operators and formulas with custom objects, the mentioned methods should be used:
Operators | = | Use Object.Equals |
<> | Use Not = | |
Formulas | EXACT | Use Object.ReferenceEquals |
TEXT | Use Convert.ToString |
The following barcode formulas can handle custom objects:
Refer to the following example code to set 2D array of custom objects to a range.
C# |
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.AutoFit(); activeSheet.Columns[0].Hidden = true; //save to a pdf file workbook.Save("setcustomrangevalue.pdf"); |
Refer to the following example code to override JSON serialization behavior.
C# |
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; } |