Esempio n. 1
0
        public static List <Dictionary <string, object> > GenerateRows(IRFDataSet dataSet)
        {
            var result  = new List <Dictionary <string, object> >();
            var rowType = dataSet.GetRowType();

            foreach (var r in dataSet.GetRows())
            {
                var row = new Dictionary <string, object>();
                foreach (var propertyInfo in rowType.GetProperties())
                {
                    if (RFReflectionHelpers.IsStruct(propertyInfo) || RFReflectionHelpers.IsMappingKey(propertyInfo))
                    {
                        var valueStruct = propertyInfo.GetValue(r);
                        foreach (var innerPropertyInfo in propertyInfo.PropertyType.GetProperties())
                        {
                            if (!row.ContainsKey(innerPropertyInfo.Name))
                            {
                                row.Add(innerPropertyInfo.Name, innerPropertyInfo.GetValue(valueStruct));
                            }
                            // also add children if no conflict, so 2 levels of drill-down
                            if (RFReflectionHelpers.IsStruct(innerPropertyInfo) || RFReflectionHelpers.IsMappingKey(innerPropertyInfo))
                            {
                                var innerStruct = innerPropertyInfo.GetValue(valueStruct);
                                foreach (var inner2PropertyInfo in innerPropertyInfo.PropertyType.GetProperties())
                                {
                                    if (!row.ContainsKey(inner2PropertyInfo.Name))
                                    {
                                        row.Add(inner2PropertyInfo.Name, inner2PropertyInfo.GetValue(innerStruct));
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        if (!row.ContainsKey(propertyInfo.Name))
                        {
                            row.Add(propertyInfo.Name, propertyInfo.GetValue(r));
                        }
                    }
                }
                result.Add(row);
            }

            return(result);
        }
Esempio n. 2
0
        public static string ExportToJSON(IRFDataSet dataSet, List <KeyValuePair <string, Type> > columnTypes)
        {
            var array = new JArray();

            // header row
            var rowType = dataSet.GetRowType();

            foreach (var r in dataSet.GetRows())
            {
                var obj = new JObject();
                foreach (var propertyInfo in rowType.GetProperties())
                {
                    if (RFReflectionHelpers.IsStruct(propertyInfo) || RFReflectionHelpers.IsMappingKey(propertyInfo))
                    {
                        var valueStruct = propertyInfo.GetValue(r);
                        foreach (var innerPropertyInfo in propertyInfo.PropertyType.GetProperties())
                        {
                            if (valueStruct != null)
                            {
                                var columnName = String.Format("{0},{1}", propertyInfo.Name, innerPropertyInfo.Name);
                                obj.Add(new JProperty(
                                            columnName,
                                            GetValue(columnName, innerPropertyInfo.GetValue(valueStruct), columnTypes)));
                            }
                        }
                    }
                    else
                    {
                        var columnName = propertyInfo.Name;
                        obj.Add(new JProperty(
                                    columnName,
                                    GetValue(columnName, propertyInfo.GetValue(r), columnTypes)));
                    }
                }
                array.Add(obj);
            }

            return(array.ToString());
        }
Esempio n. 3
0
        /*private static void AddToWorkbook(XSSFWorkbook wb, string sheetName, IRFDataSet dataSet)
         * {
         *  AddToWorkbook(wb, sheetName, dataSet.GetRows());
         * }*/

        private static void AddToWorkbook(XSSFWorkbook wb, string sheetName, IEnumerable <IRFDataRow> rows)
        {
            var sheet = wb.CreateSheet(sheetName);
            var cH    = wb.GetCreationHelper();

            var cellStyles = new Dictionary <string, ICellStyle>();
            var dataFormat = wb.CreateDataFormat();
            var dateStyle  = wb.CreateCellStyle();

            dateStyle.DataFormat = dataFormat.GetFormat("yyyy-MMM-dd");
            cellStyles.Add("date", dateStyle);

            if (rows.Any())
            {
                // header row
                var rowType   = rows.First().GetType();
                var headerRow = sheet.CreateRow(0);
                int colNo     = 0;
                foreach (var propertyInfo in rowType.GetProperties())
                {
                    if (RFReflectionHelpers.IsStruct(propertyInfo) || RFReflectionHelpers.IsMappingKey(propertyInfo))
                    {
                        foreach (var innerPropertyInfo in propertyInfo.PropertyType.GetProperties())
                        {
                            var cell = headerRow.CreateCell(colNo);
                            cell.SetCellValue(String.Format("{0}.{1}", propertyInfo.Name, innerPropertyInfo.Name));
                            colNo++;
                        }
                    }
                    else
                    {
                        var cell = headerRow.CreateCell(colNo);
                        cell.SetCellValue(propertyInfo.Name);
                        colNo++;
                    }
                }

                int rowNo = 1;
                foreach (var r in rows)
                {
                    var dataRow = sheet.CreateRow(rowNo);
                    colNo = 0;
                    foreach (var propertyInfo in rowType.GetProperties())
                    {
                        if (RFReflectionHelpers.IsStruct(propertyInfo) || RFReflectionHelpers.IsMappingKey(propertyInfo))
                        {
                            var valueStruct = propertyInfo.GetValue(r);
                            foreach (var innerPropertyInfo in propertyInfo.PropertyType.GetProperties())
                            {
                                var cell = dataRow.CreateCell(colNo);
                                if (valueStruct != null)
                                {
                                    SetValue(cell, innerPropertyInfo.GetValue(valueStruct), cellStyles);
                                }
                                colNo++;
                            }
                        }
                        else
                        {
                            var cell = dataRow.CreateCell(colNo);
                            SetValue(cell, propertyInfo.GetValue(r), cellStyles);
                            colNo++;
                        }
                    }
                    rowNo++;
                }
            }
        }