最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#實(shí)現(xiàn)Json轉(zhuǎn)DataTable并導(dǎo)出Excel的方法示例

 更新時(shí)間:2019年02月12日 10:12:02   作者:東邊的小山  
這篇文章主要介紹了C#實(shí)現(xiàn)Json轉(zhuǎn)DataTable并導(dǎo)出Excel的方法,結(jié)合實(shí)例形式總結(jié)分析了Json轉(zhuǎn)換DataTable,以及DataTable導(dǎo)出Excel相關(guān)操作技巧,需要的朋友可以參考下

本文實(shí)例講述了C#實(shí)現(xiàn)Json轉(zhuǎn)DataTable并導(dǎo)出Excel的方法。分享給大家供大家參考,具體如下:

需求:有一個(gè)log文件,需要整理成Excel,日志文件里面的數(shù)據(jù)都是json字符串

思路是,把Json字符串轉(zhuǎn)換成DataTable,然后導(dǎo)出到Excel

在網(wǎng)上找了一些資料,整理了以下三種類型的Json

一、Json轉(zhuǎn)換DataTable

1.處理簡(jiǎn)單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 字符串 轉(zhuǎn)換為 DataTable數(shù)據(jù)集合
/// </summary>
/// <param name="json"></param>
/// <returns></returns>
public static DataTable ToDataTableTwo(string json)
{
  DataTable dataTable = new DataTable(); //實(shí)例化
  DataTable result;
  try
  {
    JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
    javaScriptSerializer.MaxJsonLength = Int32.MaxValue; //取得最大數(shù)值
    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); //循環(huán)添加行到DataTable中
      }
    }
  }
  catch
  {
  }
  result = dataTable;
  return result;
}

2.處理復(fù)雜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 字符串 轉(zhuǎn)換為 DataTable數(shù)據(jù)集合
/// </summary>
/// <param name="json"></param>
/// <returns></returns>
public static DataTable ToDataTable(string json)
{
  DataTable dataTable = new DataTable(); //實(shí)例化
  DataTable result;
  try
  {
    JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
    javaScriptSerializer.MaxJsonLength = Int32.MaxValue; //取得最大數(shù)值
    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;
}

3.處理不規(guī)則Json,因?yàn)榱胁⒉淮_定,所以直接定義列,不動(dòng)態(tài)生成

[{"id":"00e58d53","data":[{"mac":"34:b3:54:89:86:64","rssi":"-86","ch":"13"},{"mac":"50:bd:5f:02:80:44","rssi":"-90","ch":"1"}]},
{"id":"00ccda81","data":[{"mac":"bc:46:99:4e:96:c8","rssi":"-92","ch":"1"},{"mac":"bc:3a:ea:fc:77:6c","rssi":"-93","ch":"6","ds":"Y","essid":"vienna hotel WIFI"}]}]

/// <summary>
/// Json 字符串 轉(zhuǎn)換為 DataTable數(shù)據(jù)集合
/// </summary>
/// <param name="json"></param>
/// <returns></returns>
public static DataTable ToDataTable(string json)
{
  DataTable dataTable = new DataTable(); //實(shí)例化
  DataTable result;
  try
  {
    dataTable.Columns.Add("id");
    dataTable.Columns.Add("mac");
    dataTable.Columns.Add("rssi");
    dataTable.Columns.Add("ch");
    dataTable.Columns.Add("ts");
    dataTable.Columns.Add("tmc");
    dataTable.Columns.Add("tc");
    dataTable.Columns.Add("ds");
    dataTable.Columns.Add("essid");
    JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
    javaScriptSerializer.MaxJsonLength = Int32.MaxValue; //取得最大數(shù)值
    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;
        }//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;
}

二、導(dǎo)出Excel

/// <summary>
/// 導(dǎo)出Excel
/// </summary>
/// <param name="table"></param>
/// <param name="file"></param>
public void dataTableToCsv(DataTable table, string file)
{
  string title = "";
  FileStream fs = new FileStream(file, FileMode.OpenOrCreate);
  StreamWriter sw = new StreamWriter(new BufferedStream(fs), System.Text.Encoding.Default);
  for (int i = 0; i < table.Columns.Count; i++)
  {
    title += table.Columns[i].ColumnName + "\t"; //欄位:自動(dòng)跳到下一單元格
  }
  title = title.Substring(0, title.Length - 1) + "\n";
  sw.Write(title);
  foreach (DataRow row in table.Rows)
  {
    string line = "";
    for (int i = 0; i < table.Columns.Count; i++)
    {
      line += row[i].ToString().Trim() + "\t"; //內(nèi)容:自動(dòng)跳到下一單元格
    }
    line = line.Substring(0, line.Length - 1) + "\n";
    sw.Write(line);
  }
  sw.Close();
  fs.Close();
}

三、調(diào)用實(shí)現(xiàn),數(shù)據(jù)導(dǎo)出到Excel

protected void Button1_Click(object sender, EventArgs e)
{
  string str = File.ReadAllText(@"C:\Users\Admin\Desktop\json.txt");
  DataTable dt = ToDataTable(str);
  this.dataTableToCsv(dt, @"E:\json.xls"); //調(diào)用函數(shù)
}

PS:關(guān)于json操作,這里再為大家推薦幾款比較實(shí)用的json在線工具供大家參考使用:

在線JSON代碼檢驗(yàn)、檢驗(yàn)、美化、格式化工具:
http://tools.jb51.net/code/json

JSON在線格式化工具:
http://tools.jb51.net/code/jsonformat

在線XML/JSON互相轉(zhuǎn)換工具:
http://tools.jb51.net/code/xmljson

json代碼在線格式化/美化/壓縮/編輯/轉(zhuǎn)換工具:
http://tools.jb51.net/code/jsoncodeformat

在線json壓縮/轉(zhuǎn)義工具:
http://tools.jb51.net/code/json_yasuo_trans

更多關(guān)于C#相關(guān)內(nèi)容還可查看本站專題:《C#字符串操作技巧總結(jié)》、《C#常見(jiàn)控件用法教程》、《WinForm控件用法總結(jié)》、《C#程序設(shè)計(jì)之線程使用技巧總結(jié)》、《C#中XML文件操作技巧匯總》、《C#數(shù)據(jù)結(jié)構(gòu)與算法教程》、《C#數(shù)組操作技巧總結(jié)》及《C#面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程

希望本文所述對(duì)大家C#程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • WPF數(shù)據(jù)綁定時(shí)出現(xiàn)StringFormat失效的原因和解決方法

    WPF數(shù)據(jù)綁定時(shí)出現(xiàn)StringFormat失效的原因和解決方法

    在數(shù)據(jù)綁定過(guò)程中,我們經(jīng)常會(huì)使用StringFormat對(duì)要顯示的數(shù)據(jù)進(jìn)行格式化,以便獲得更為直觀的展示效果,但在某些情況下格式化操作并未生效,所以本文介紹了WPF數(shù)據(jù)綁定時(shí)出現(xiàn)StringFormat失效的原因和解決方法,需要的朋友可以參考下
    2024-12-12
  • C#中執(zhí)行SQL的幾種方法講解

    C#中執(zhí)行SQL的幾種方法講解

    這篇文章介紹了C#中執(zhí)行SQL的幾種方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-04-04
  • Unity AssetBundle打包工具示例詳解

    Unity AssetBundle打包工具示例詳解

    這篇文章主要介紹了Unity AssetBundle打包工具,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-10-10
  • C#日期轉(zhuǎn)換函數(shù)分享

    C#日期轉(zhuǎn)換函數(shù)分享

    這篇文章介紹了C#日期轉(zhuǎn)換函數(shù),有需要的朋友可以參考一下
    2013-10-10
  • C#中委托的基本概念介紹

    C#中委托的基本概念介紹

    這篇文章主要介紹了C#中委托的基本概念介紹,本文講解了委托的使用、委托類型和委托實(shí)例、委托的合并和刪除、委托是不易變的、委托調(diào)用列表、GetInvocationList等內(nèi)容,需要的朋友可以參考下
    2015-02-02
  • C#中單例模式的三種寫法示例

    C#中單例模式的三種寫法示例

    這篇文章主要介紹了C#中單例模式的三種寫法示例,本文分別給出代碼實(shí)例,需要的朋友可以參考下
    2015-06-06
  • 提高C# StringBuilder操作性能優(yōu)化的方法

    提高C# StringBuilder操作性能優(yōu)化的方法

    本篇文章主要介紹使用C# StringBuilder 的項(xiàng)目實(shí)踐,用于減少內(nèi)存分配,提高字符串操作的性能。對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-11-11
  • C#實(shí)現(xiàn)備忘錄功能

    C#實(shí)現(xiàn)備忘錄功能

    這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)備忘錄功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-11-11
  • C# Onnx實(shí)現(xiàn)DIS高精度圖像二類分割

    C# Onnx實(shí)現(xiàn)DIS高精度圖像二類分割

    這篇文章主要為大家詳細(xì)介紹了C# Onnx實(shí)現(xiàn)DIS高精度圖像二類分割的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2023-11-11
  • c#實(shí)現(xiàn)在圖上畫漢字

    c#實(shí)現(xiàn)在圖上畫漢字

    這篇文章主要介紹了c#實(shí)現(xiàn)在圖上畫漢字方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-02-02

最新評(píng)論

瑞昌市| 万年县| 东至县| 黎城县| 大邑县| 巴青县| 资兴市| 昭觉县| 龙川县| 沙湾县| 五指山市| 万全县| 扶风县| 开封市| 平和县| 寿宁县| 元江| 新乐市| 阿图什市| 嘉兴市| 通城县| 华亭县| 台北县| 普宁市| 云阳县| 盐津县| 沁阳市| 孝义市| 开封市| 汝阳县| 紫阳县| 黎平县| 山丹县| 华坪县| 黔西| 延川县| 肥西县| 隆林| 绥棱县| 克拉玛依市| 桃江县|