c# json转DataTable导成Excel格式,下载到浏览器

简单的json格式转换DataTable

示例:
[{“id”:“3189”,“group”:“在校GPA”,“project”:“期中考试”,“goal”:“1”},
{“id”:“3190”,“group”:“在校GPA”,“project”:“期末考试”,“goal”:“2”},
{“id”:“3191”,“group”:“在校GPA”,“project”:“毕业会考”,“goal”:“3”},]

Step1、json转换dataTable

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;
        }

输出结果:
在这里插入图片描述
Step2、dataTable导出Excel表格并下载到本地

 public void CreateExcel(string json)
        {
           string   FileName ="新下载";
            if (!string.IsNullOrEmpty(FileName))
            {
                System.Data.DataTable dt = ToDataTableTwo(json);
                Response.Clear();
                Response.Charset = "UTF-8";
                Response.Buffer = true;
                Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
                Response.AppendHeader("Content-Disposition", "attachment;filename=\"" + System.Web.HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8) + ".xls\"");
                Response.ContentType = "application/ms-excel";
                string colHeaders = string.Empty;
                string ls_item = string.Empty;
                System.Data.DataRow[] myRow = dt.Select();
                int i = 0;
                int cl = dt.Columns.Count;
                foreach (System.Data.DataRow row in myRow)
                {
                    for (i = 0; i < cl; i++)
                    {
                        if (i == (cl - 1))
                        {
                            ls_item += row[i].ToString() + "\n";
                        }
                        else
                        {
                            ls_item += row[i].ToString() + "\t";
                        }
                    }
                    Response.Output.Write(ls_item);
                    ls_item = string.Empty;
                }
                Response.Output.Flush();
                Response.End();
            }
        }

结果
在这里插入图片描述
Ending…

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值