示例#1
0
        protected override void Seed(AppWithEF.DatabaseContext context)
        {
            //  This method will be called after migrating to the latest version.

            //  You can use the DbSet<T>.AddOrUpdate() helper extension method
            //  to avoid creating duplicate seed data.

            if (!context.Albums.Any())
            {
                for (int i = 0; i < 10; i++)
                {
                    context.Albums.Add(new Album
                    {
                        Name        = NameData.GetCompanyName(),
                        Description = TextData.GetSentence(),
                        Duration    = NumberData.GetNumber(),
                        Author      = NameData.GetFullName()
                    });
                }

                context.SaveChanges();
            }
            else
            {
                context.Albums.ToList().ForEach(x =>
                {
                    if (x.Duration == null || x.Duration == 0)
                    {
                        x.Duration = NumberData.GetNumber(500, 1000);
                    }

                    if (string.IsNullOrEmpty(x.Author))
                    {
                        x.Author = NameData.GetFullName();
                    }
                });

                if (context.ChangeTracker.HasChanges())
                {
                    context.SaveChanges();
                }
            }
        }
示例#2
0
        public IActionResult ExcelExport()
        {
            // https://steemit.com/utopian-io/@haig/how-to-create-excel-spreadsheets-using-npoi

            var workbook = new HSSFWorkbook();
            var sheet    = workbook.CreateSheet("Sheet 1");

            IFont fontBold = workbook.CreateFont();

            fontBold.Boldweight = (short)FontBoldWeight.Bold;

            var headerCellStyle = workbook.CreateCellStyle();

            headerCellStyle.SetFont(fontBold);

            var   header = sheet.CreateRow(0);
            ICell col1   = header.CreateCell(0); col1.CellStyle = headerCellStyle; col1.SetCellValue("Fullname");
            ICell col2   = header.CreateCell(1); col2.CellStyle = headerCellStyle; col2.SetCellValue("Country");
            ICell col3   = header.CreateCell(2); col3.CellStyle = headerCellStyle; col3.SetCellValue("Email");
            ICell col4   = header.CreateCell(3); col4.CellStyle = headerCellStyle; col4.SetCellValue("Numbers");
            ICell col5   = header.CreateCell(4); col5.CellStyle = headerCellStyle; col5.SetCellValue("Datetime");
            ICell col6   = header.CreateCell(5); col6.CellStyle = headerCellStyle; col6.SetCellValue("Sentence");

            var currencyCellStyle = workbook.CreateCellStyle();

            currencyCellStyle.Alignment  = HorizontalAlignment.Right;
            currencyCellStyle.DataFormat = workbook.CreateDataFormat().GetFormat("₺#.##0,00");;

            int length = 50;

            for (int i = 0; i < length; i++)
            {
                var row = sheet.CreateRow(i + 1);
                row.CreateCell(0).SetCellValue(NameData.GetFullName());
                row.CreateCell(1).SetCellValue(PlaceData.GetCountry());
                row.CreateCell(2).SetCellValue(NetworkData.GetEmail());

                ICell numberCell = row.CreateCell(3);
                numberCell.CellStyle = currencyCellStyle;
                numberCell.SetCellValue(NumberData.GetDouble() * 1000);

                row.CreateCell(4).SetCellValue(DateTimeData.GetDatetime().ToShortDateString());
                row.CreateCell(5).SetCellValue(TextData.GetSentence());
            }

            var   totalRow  = sheet.CreateRow(length + 1);
            ICell totalCell = totalRow.CreateCell(3);

            ICellStyle totalCellStyle = workbook.CreateCellStyle();

            totalCellStyle.CloneStyleFrom(sheet.GetRow(length).GetCell(3).CellStyle);

            totalCell.CellStyle = totalCellStyle;
            totalCell.CellStyle.SetFont(fontBold);
            totalCell.SetCellType(CellType.Formula);
            totalCell.CellFormula = $"SUM(D2:D{length + 1})";   // Headercell den sonrası.

            string root     = _environment.WebRootPath;
            string folder   = Path.Combine(root, "Excels");
            string filename = "f_" + Guid.NewGuid().ToString() + ".xls";
            string file     = Path.Combine(folder, filename);

            if (Directory.Exists(folder) == false)
            {
                Directory.CreateDirectory(folder);
            }

            using (var fileStream = new FileStream(file, FileMode.Create))
            {
                workbook.Write(fileStream);
            }

            return(File("~/Excels/" + filename, "application/vnd.ms-excel"));
        }