如何将 JsonResult 转换为 ClosedXml?
How to convert JsonResult to ClosedXml?
我有一个 JSON 格式的数据集。但是如何将其写入 ClosedXML 格式?
dataSet.Data { total = "123456", data = Count = 11, model '{\fields":{"DocumentId":{"title""...
private void SetWorkbook(IXLWorkbook workbook, JsonResult dataSet)
{
var worksheet = workbook.Worksheets.Add("Export");
DataTable dataTable = (DataTable)JsonConvert.DeserializeObject(dataSet.Data, (typeof(DataTable)));
....
}
使用 PropertyInfo 和 Dictionary,您可以捕获 JSON-data。
private Dictionary<string, string> GetDataSet(object dataSet)
{
Type type = dataSet.GetType();
System.Reflection.PropertyInfo[] properties = type.GetProperties();
foreach (var property in properties)
{
if (property.Name == "data")
{
Dictionary<string, string> dictionary = new Dictionary<string, string>();
dynamic data = property.GetValue(dataSet);
foreach (var rows in data)
{
foreach (var row in rows)
{
string value = string.Empty;
if (dictionary.ContainsKey(row.Key))
value = dictionary[row.Key] + "," + row.Value;
else
value = row.Value;
dictionary[row.Key] = value;
}
}
return dictionary;
}
}
return new Dictionary<string, string>();
}
我有一个 JSON 格式的数据集。但是如何将其写入 ClosedXML 格式?
dataSet.Data { total = "123456", data = Count = 11, model '{\fields":{"DocumentId":{"title""...
private void SetWorkbook(IXLWorkbook workbook, JsonResult dataSet)
{
var worksheet = workbook.Worksheets.Add("Export");
DataTable dataTable = (DataTable)JsonConvert.DeserializeObject(dataSet.Data, (typeof(DataTable)));
....
}
使用 PropertyInfo 和 Dictionary,您可以捕获 JSON-data。
private Dictionary<string, string> GetDataSet(object dataSet)
{
Type type = dataSet.GetType();
System.Reflection.PropertyInfo[] properties = type.GetProperties();
foreach (var property in properties)
{
if (property.Name == "data")
{
Dictionary<string, string> dictionary = new Dictionary<string, string>();
dynamic data = property.GetValue(dataSet);
foreach (var rows in data)
{
foreach (var row in rows)
{
string value = string.Empty;
if (dictionary.ContainsKey(row.Key))
value = dictionary[row.Key] + "," + row.Value;
else
value = row.Value;
dictionary[row.Key] = value;
}
}
return dictionary;
}
}
return new Dictionary<string, string>();
}