C# 将DataTable转换为Json格式,如需讲Excel转为Json可先将Excel转为DataTable
public static string DataTableToJson(DataTable dt) { var json = new JObject(); var table = new JArray() as dynamic; foreach (DataRow dataRow in dt.Rows) { dynamic row = new JObject(); foreach (DataColumn column in dt.Columns) { row.Add(column.ColumnName, dataRow[column.ColumnName].ToString()); } table.Add(row); } json.Add("data", table); return json.ToString(Formatting.None); }
评论前必须登录!
注册