Exemplo n.º 1
0
        /// <summary>
        /// Preenche o banco dados com 1000 alunos aletórios diferentes
        /// cada um com o seu conjunto de notas gerado aletorimente
        /// </summary>
        public static void startOperation()
        {
            Random rand = new Random();

            int tryOut = 50;
            int count  = 1000;

            while (count > 0)
            {
                List <int>    posNomes = new List <int>();
                List <Double> notas    = new List <double>();
                int           pos;

                if (tryOut == 0)
                {
                    break;
                }

                //Pega 3 No diferentes aleatorios
                for (int i = 0; i < 3; i++)
                {
                    do
                    {
                        pos = rand.Next(0, 31);
                    }while(posNomes.Contains(pos));

                    posNomes.Add(pos);
                }

                for (int i = 0; i < 9; i++)
                {
                    double nota = (Convert.ToDouble(rand.Next(0, 10)) + rand.NextDouble());

                    if (nota > 10)
                    {
                        nota = 10;
                    }

                    notas.Add(Math.Round(nota, 2));
                }

                string nomeAluno = String.Format("{0} {1} {2}", nomeArray[posNomes[0]],
                                                 nomeArray[posNomes[1]], nomeArray[posNomes[2]]);

                long alunoId = AlunoProcessor.CreateAluno(nomeAluno);

                if (alunoId == -1)
                {
                    tryOut--;
                }
                else
                {
                    NotasProcessor.CreateNota(alunoId, notas[0],
                                              notas[1], notas[2], notas[3], notas[4],
                                              notas[5], notas[6], notas[7], notas[8]);

                    tryOut = 50;
                    count--;
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Gera a tela no formato XLSX
        /// </summary>
        /// <returns>Memória contendo o dado da planilha</returns>
        public static MemoryStream Export()
        {
            List <AlunoModel> alunos = AlunoProcessor.ListAluno();
            List <NotasModel> notas  = NotasProcessor.ListNotas();

            NotasModel medias = new NotasModel();

            medias.Matematica = 0;
            medias.Portugues  = 0;
            medias.Historia   = 0;
            medias.Geografia  = 0;
            medias.Ingles     = 0;
            medias.Biologia   = 0;
            medias.Filosofia  = 0;
            medias.Fisica     = 0;
            medias.Quimica    = 0;

            try
            {
                using (var workbook = new XLWorkbook())
                {
                    //Gera op caebçalho da tabela
                    IXLWorksheet worksheet =
                        workbook.Worksheets.Add("Notas Alunos");
                    worksheet.Cell(1, 1).Value  = "Nome Aluno";
                    worksheet.Cell(1, 2).Value  = "Matemática";
                    worksheet.Cell(1, 3).Value  = "Português";
                    worksheet.Cell(1, 4).Value  = "História";
                    worksheet.Cell(1, 5).Value  = "Geografia";
                    worksheet.Cell(1, 6).Value  = "Inglês";
                    worksheet.Cell(1, 7).Value  = "Biologia";
                    worksheet.Cell(1, 8).Value  = "Filosofia";
                    worksheet.Cell(1, 9).Value  = "Física";
                    worksheet.Cell(1, 10).Value = "Química";

                    int index = 1;

                    //Adiciona todos as notas do banco de dados
                    for (; index <= alunos.Count; index++)
                    {
                        worksheet.Cell(index + 1, 1).Value  = alunos[index - 1].Nome;
                        worksheet.Cell(index + 1, 2).Value  = notas[index - 1].Matematica;
                        worksheet.Cell(index + 1, 3).Value  = notas[index - 1].Portugues;
                        worksheet.Cell(index + 1, 4).Value  = notas[index - 1].Historia;
                        worksheet.Cell(index + 1, 5).Value  = notas[index - 1].Geografia;
                        worksheet.Cell(index + 1, 6).Value  = notas[index - 1].Ingles;
                        worksheet.Cell(index + 1, 7).Value  = notas[index - 1].Biologia;
                        worksheet.Cell(index + 1, 8).Value  = notas[index - 1].Filosofia;
                        worksheet.Cell(index + 1, 9).Value  = notas[index - 1].Fisica;
                        worksheet.Cell(index + 1, 10).Value = notas[index - 1].Quimica;

                        medias.Matematica += notas[index - 1].Matematica;
                        medias.Portugues  += notas[index - 1].Portugues;
                        medias.Historia   += notas[index - 1].Historia;
                        medias.Geografia  += notas[index - 1].Geografia;
                        medias.Ingles     += notas[index - 1].Ingles;
                        medias.Biologia   += notas[index - 1].Biologia;
                        medias.Filosofia  += notas[index - 1].Filosofia;
                        medias.Fisica     += notas[index - 1].Fisica;
                        medias.Quimica    += notas[index - 1].Quimica;
                    }

                    //Gera a média das notas
                    worksheet.Cell(index + 1, 1).Value  = " Medias";
                    worksheet.Cell(index + 1, 2).Value  = medias.Matematica / alunos.Count();
                    worksheet.Cell(index + 1, 3).Value  = medias.Portugues / alunos.Count();
                    worksheet.Cell(index + 1, 4).Value  = medias.Historia / alunos.Count();
                    worksheet.Cell(index + 1, 5).Value  = medias.Geografia / alunos.Count();
                    worksheet.Cell(index + 1, 6).Value  = medias.Ingles / alunos.Count();
                    worksheet.Cell(index + 1, 7).Value  = medias.Biologia / alunos.Count();
                    worksheet.Cell(index + 1, 8).Value  = medias.Filosofia / alunos.Count();
                    worksheet.Cell(index + 1, 9).Value  = medias.Fisica / alunos.Count();
                    worksheet.Cell(index + 1, 10).Value = medias.Quimica / alunos.Count();


                    //Retorna o stram de dados
                    using (var stream = new MemoryStream())
                    {
                        workbook.SaveAs(stream);

                        return(stream);
                    }
                }
            }
            catch (Exception ex)
            {
                return(null);
            }
        }