Exemplo n.º 1
0
        public static ExcelModel ConvertDictionaryToExcelModel(IEnumerable <Dictionary <string, object> > data,
                                                               ExcelExporterOptions options)
        {
            var sheetTitle = options.SheetTitle;
            var excel      = new ExcelModel {
                Sheets = new List <SheetModel>()
            };
            var sheet = SheetModel.Create(sheetTitle);

            excel.Sheets.Add(sheet);
            var list  = data.ToList();
            var title = list.FirstOrDefault();

            if (title == null)
            {
                return(excel);
            }
            var columns = title.Keys.Select((c, i) => new ExcelColumn
            {
                Order = i,
                Title = c,
                Type  = typeof(string)
            }).ToList();

            sheet.Columns = AttachColumns(columns, options);
            sheet.Rows    = list;

            return(excel);
        }
Exemplo n.º 2
0
        public static ExcelModel ConvertObjectToExcelModel <TModel>(IEnumerable <TModel> data,
                                                                    ExcelExporterOptions options)
        {
            var sheetTitle = options.SheetTitle;
            var excel      = new ExcelModel {
                Sheets = new List <SheetModel>()
            };

            if (string.IsNullOrWhiteSpace(sheetTitle))
            {
                var classAttr = ExcelUtil.GetClassExportAttribute <TModel>();
                sheetTitle = classAttr.Item1 == null ? sheetTitle : classAttr.Item1.Title;
            }

            var sheet = SheetModel.Create(sheetTitle);

            excel.Sheets.Add(sheet);
            var attrDict     = ExcelUtil.GetPropertiesAttributesDict <TModel>();
            var objKeysArray = attrDict.OrderBy(c => c.Value.ExcelTitleAttribute.Order).ToArray();

            var columns = new List <ExcelColumn>();

            for (var i = 0; i < objKeysArray.Length; i++)
            {
                var titleAttr = objKeysArray[i].Value.ExcelTitleAttribute;
                var fontAttr  = objKeysArray[i].Value.ExcelColumnFontAttribute;
                var column    = new ExcelColumn
                {
                    Title = titleAttr.Title,
                    Type  = objKeysArray[i].Key.PropertyType,
                    Order = i,
                    Font  = fontAttr == null ? null : new Font(fontAttr.FontName, fontAttr.FontHeightInPoints, fontAttr.Color, fontAttr.IsBold),
                };

                columns.Add(column);
            }

            sheet.Columns = AttachColumns(columns, options);
            foreach (var item in data.Where(c => c != null))
            {
                var row = new Dictionary <string, object>();
                foreach (var column in objKeysArray)
                {
                    var prop = column.Key;
                    row[column.Value.ExcelTitleAttribute.Title] = prop.GetValue(item, null);
                }

                sheet.Rows.Add(row);
            }

            return(excel);
        }
Exemplo n.º 3
0
        internal static ExcelModel ConvertDataSetToExcelModel(DataTable dt, ExcelExporterOptions options)
        {
            var sheetTitle = options.SheetTitle;
            var excel      = new ExcelModel {
                Sheets = new List <SheetModel>()
            };
            var sheet = SheetModel.Create(sheetTitle);

            excel.Sheets.Add(sheet);
            var dataSetColumnArray = dt.Columns.Cast <DataColumn>().ToArray();

            var columns = dataSetColumnArray.Select((item, i) =>
                                                    new ExcelColumn
            {
                Order = i,
                Title = item.ColumnName,
                Type  = item.DataType
            }).ToList();

            sheet.Columns = AttachColumns(columns, options);

            var data = dt.Rows.Cast <DataRow>().ToArray();

            foreach (var item in data.Where(c => c != null))
            {
                var row = new Dictionary <string, object>();
                foreach (var column in dataSetColumnArray)
                {
                    row[column.ColumnName] = item[column.ColumnName];
                }

                sheet.Rows.Add(row);
            }

            return(excel);
        }