public byte[] Export <T1>(ExportModel <T1> item2) { using (var p = new ExcelPackage()) { CreateSheet(item2.propAndheader, item2.list, item2.sheetName, p); return(p.GetAsByteArray()); } }
public byte[] ToExcel(string path) { var jsonObj = JsonConvert.DeserializeObject(File.ReadAllText(path)); var jsonObjType = jsonObj.GetType(); if (jsonObjType == typeof(JObject)) { var obj = (JObject)jsonObj; if (options.ListMode) { JArray array = FindJArray(obj); if (array == null) { throw new Exception(Properties.Resources.Ex_UnsupportedFormat); } return(ExportJArray(array)); } else { var jList = (IEnumerable <KeyValuePair <string, JToken> >)jsonObj; //获取list<KeyValueModel> var list = jList.Select(t => { var item = t.Value.ToObject <dynamic>(); var model = new KeyValueModel(t.Key, item); return(model); }).ToList(); var exportModel = new ExportModel <KeyValueModel> { list = list, propAndheader = headerMap1, sheetName = "Sheet1" }; var service = new ExportService(); return(service.Export(exportModel)); } } else if (jsonObjType == typeof(JArray)) { return(ExportJArray((JArray)jsonObj)); } throw new Exception(Properties.Resources.Ex_UnsupportedFormat); }
private byte[] ExportJArray(JArray array) { var list = array.Children(); var first = list.First(); var firstType = first.GetType(); Dictionary <string, string> headerMap = null; IEnumerable <Dictionary <string, object> > exportList = null; if (firstType == typeof(JValue)) { headerMap = headerMap2; exportList = list.Select(t => { Dictionary <string, object> map1 = new Dictionary <string, object>(); map1.Add("Value", t.ToObject <dynamic>()); return(map1); }); } else { headerMap = first.Children().Select(t => (JProperty)t).ToDictionary(t => t.Name, t => t.Name); exportList = list.Select(t => { Dictionary <string, object> map1 = new Dictionary <string, object>(); foreach (var item in t.Children()) { var p1 = (JProperty)item; map1.Add(p1.Name, p1.Value.ToObject <dynamic>()); } return(map1); }); } var exportModel = new ExportModel { list = exportList, propAndheader = headerMap, sheetName = "Sheet1" }; var service = new ExportService(); return(service.Export(exportModel)); }