public void Serialize(object objectToSerialize, string path) { var sheetName = "Sheet1"; var ve = new ValueExtractor(); ve.Extract(objectToSerialize); using (Package = new ExcelPackage()) { Package.Workbook.Properties.Author = Author; Package.Workbook.Properties.Title = Title; Sheet = Package.Workbook.Worksheets.Add(sheetName); GetHeader(); var dvw = new DirectValueWriter(ve.DirectValues, Sheet, 2, 1); dvw.Run(); var ow = new ObjectWriter(ve.Objects, Sheet, 4, 1); ow.Run(); var olw = new ObjectListWriter(ve.ObjectLists, Sheet, 8, 1); olw.Run(); Package.SaveAs(new System.IO.FileInfo(path)); ////Create excel headers //Sheet.Cells[2, 1].Value = "Owner"; //Sheet.Cells[2, 2].Value = "Link Id"; //Sheet.Cells[2, 3].Value = "Link Name"; //Sheet.Cells[2, 4].Value = "Date"; //sheet.Row(2).Style.Font.Bold = true; //var rowIndex = 3; //foreach ( var downloadLink in downloadLinks ) //{ // var col = 1; // sheet.Cells[rowIndex, col++].Value = downloadLink.OwnerLogonName; // sheet.Cells[rowIndex, col++].Value = downloadLink.Id; // sheet.Cells[rowIndex, col++].Value = downloadLink.LinkName; // sheet.Cells[rowIndex, col++].Value = downloadLink.CreateDate; // rowIndex++; // sheet.Cells["B" + rowIndex + ":K" + rowIndex].Merge = true; // sheet.Cells[rowIndex, 1].Value = "Paths"; // sheet.Cells[rowIndex, 1].Style.Font.Bold = true; // sheet.Cells[rowIndex, 1].Style.VerticalAlignment = OfficeOpenXml.Style.ExcelVerticalAlignment.Top; // var paths = JsonConvert.DeserializeObject<List<string>>(downloadLink.Recipe); // sheet.Cells[rowIndex, 2].Value = string.Join("\r\n", paths); // sheet.Row(rowIndex).Height *= paths.Count; // sheet.Row(rowIndex).Style.WrapText = true; // rowIndex++; // sheet.Cells["B" + rowIndex + ":K" + rowIndex].Merge = true; // sheet.Cells[rowIndex, 1].Value = "Receivers"; // sheet.Cells[rowIndex, 1].Style.Font.Bold = true; // var receivers = JsonConvert.DeserializeObject<List<user>>(downloadLink.SentUsersByDesktopAgent); // sheet.Cells[rowIndex, 2].Value = string.Join("\r\n", receivers.Select(r => string.Format("{0} - {1}({2})", r.isGroup ? "group" : "user", r.name, r.id))); // sheet.Row(rowIndex).Height *= receivers.Count; // sheet.Row(rowIndex).Style.WrapText = true; // rowIndex++; //} //sheet.Column(1).AutoFit(); //sheet.Column(2).AutoFit(); //sheet.Column(3).AutoFit(); //sheet.Column(4).AutoFit(); //sheet.Column(4).Style.Numberformat.Format = "dd/mm/yyyy"; //ReturnFile(fileName, Package, sheet); } }
public void Extract(object instance) { if (instance == null) { return; } IList <PropertyInfo> props = new List <PropertyInfo>(instance.GetType().GetProperties()); foreach (PropertyInfo prop in props) { var t = prop.PropertyType; // direct value if (t.IsPrimitive || t.IsValueType || (t == typeof(string))) { DirectValues.Add(prop.Name, new ObjectWrapper { ObjectType = ObjectTypes.DirectValue, Value = prop.GetValue(instance) }); } else if (t.GetInterface("ICollection") != null) { var collection = prop.GetValue(instance) as ICollection; //bool isValid = collection.Select(x => ((dynamic)x).GetType()).Distinct().Count() == 1; //if ( !isValid ) continue; var objectWrapper = new ObjectWrapper { ObjectType = ObjectTypes.ObjectList, Value = new List <object>() }; var ve = new ValueExtractor(); foreach (var item in collection) { (objectWrapper.Value as List <object>).Add(item); } ObjectLists.Add(prop.Name, objectWrapper); } else if (t.GetInterface("IEnumerable") != null) { var enumerable = prop.GetValue(instance) as IEnumerable <object>; var isValid = enumerable.Select(x => x.GetType()).Distinct().Count() == 1; if (!isValid) { continue; } Console.WriteLine(prop.Name); //var ve = new ValueExtractor(); //foreach ( var item in enumerable ) //{ // var xs = new ValueExtractor(); // xs.Extract(prop.GetValue(item)); // Objects.Add(prop.Name, new ObjectWrapper // { // ObjectType = ObjectTypes.Object, // Value = xs.DirectValues // }); //} } // object else { var xs = new ValueExtractor(); xs.Extract(prop.GetValue(instance)); Objects.Add(prop.Name, new ObjectWrapper { ObjectType = ObjectTypes.Object, Value = xs.DirectValues }); } } }