C#实现Json转DataTable并导出Excel的方法示例

2019-09-13 12:03:37王振洲

本文实例讲述了C#实现Json转DataTable并导出Excel的方法。,具体如下:

需求:有一个log文件,需要整理成Excel,日志文件里面的数据都是json字符串

思路是,把Json字符串转换成DataTable,然后导出到Excel

在网上找了一些资料,整理了以下三种类型的Json

一、Json转换DataTable

1.处理简单Json:

[{"mac":"20:f1:7c:c5:cd:80","rssi":"-86","ch":"9"},{"mac":"20:f1:7c:c5:cd:85","rssi":"-91","ch":"9"}]

/// <summary>
/// Json 字符串 转换为 DataTable数据集合
/// </summary>
/// <param name="json"></param>
/// <returns></returns>
public static DataTable ToDataTableTwo(string json)
{
  DataTable dataTable = new DataTable(); //实例化
  DataTable result;
  try
  {
    JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
    javaScriptSerializer.MaxJsonLength = Int32.MaxValue; //取得最大数值
    ArrayList arrayList = javaScriptSerializer.Deserialize<ArrayList>(json);
    if (arrayList.Count > 0)
    {
      foreach (Dictionary<string, object> dictionary in arrayList)
      {
        if (dictionary.Keys.Count<string>() == 0)
        {
          result = dataTable;
          return result;
        }
        //Columns
        if (dataTable.Columns.Count == 0)
        {
          foreach (string current in dictionary.Keys)
          {
            dataTable.Columns.Add(current, dictionary[current].GetType());
          }
        }
        //Rows
        DataRow dataRow = dataTable.NewRow();
        foreach (string current in dictionary.Keys)
        {
          dataRow[current] = dictionary[current];
        }
        dataTable.Rows.Add(dataRow); //循环添加行到DataTable中
      }
    }
  }
  catch
  {
  }
  result = dataTable;
  return result;
}

2.处理复杂Json

[{"id":"00e58d51","data":[{"mac":"20:f1:7c:c5:cd:80","rssi":"-86","ch":"9"},{"mac":"20:f1:7c:c5:cd:85","rssi":"-91","ch":"9"}]},
{"id":"00e58d53","data":[{"mac":"bc:d1:77:8e:26:78","rssi":"-94","ch":"11"},{"mac":"14:d1:1f:3e:bb:ac","rssi":"-76","ch":"11"},{"mac":"20:f1:7c:d4:05:41","rssi":"-86","ch":"12"}]}]

/// <summary>
/// Json 字符串 转换为 DataTable数据集合
/// </summary>
/// <param name="json"></param>
/// <returns></returns>
public static DataTable ToDataTable(string json)
{
  DataTable dataTable = new DataTable(); //实例化
  DataTable result;
  try
  {
    JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
    javaScriptSerializer.MaxJsonLength = Int32.MaxValue; //取得最大数值
    ArrayList arrayList = javaScriptSerializer.Deserialize<ArrayList>(json);
    if (arrayList.Count > 0)
    {
      foreach (Dictionary<string, object> dictionary in arrayList)
      {
        if (dictionary.Keys.Count<string>() == 0)
        {
          result = dataTable;
          return result;
        }
        //Columns
        if (dataTable.Columns.Count == 0)
        {
          foreach (string current in dictionary.Keys)
          {
            if (current != "data")
              dataTable.Columns.Add(current, dictionary[current].GetType());
            else
            {
              ArrayList list = dictionary[current] as ArrayList;
              foreach (Dictionary<string, object> dic in list)
              {
                foreach (string key in dic.Keys)
                {
                  dataTable.Columns.Add(key, dic[key].GetType());
                }
                break;
              }
            }
          }
        }
        //Rows
        string root = "";
        foreach (string current in dictionary.Keys)
        {
          if (current != "data")
            root = current;
          else
          {
            ArrayList list = dictionary[current] as ArrayList;
            foreach (Dictionary<string, object> dic in list)
            {
              DataRow dataRow = dataTable.NewRow();
              dataRow[root] = dictionary[root];
              foreach (string key in dic.Keys)
              {
                dataRow[key] = dic[key];
              }
              dataTable.Rows.Add(dataRow);
            }
          }
        }
      }
    }
  }
  catch
  {
  }
  result = dataTable;
  return result;
}