示例#1
0
        private void btnConfirmar_Click(object sender, EventArgs e)
        {
            using (var arquivo = new ExcelPackage(new FileInfo(caminhoArquivo)))
                using (var worksheet = arquivo.Workbook.Worksheets[1])
                    using (var conexao = new Connection(Database.Local))
                    {
                        int i, qtdeIgnorados = 0;
                        int colCount = worksheet.Dimension.End.Column;
                        int rowCount = worksheet.Dimension.End.Row;

                        worksheet.Cells[1, colCount + 1].Value = "Mensagem Importação";

                        for (i = 2; i <= rowCount; i++)
                        {
                            ImportacaoClienteTO dadosCliente = lstDados[i - 2];

                            if (dadosCliente.Ignorar)
                            {
                                worksheet.Cells[i, colCount + 1].Value = dadosCliente.Mensagem;
                                qtdeIgnorados++;
                                continue;
                            }

                            List <Pergunta> perguntas = Pergunta.GetAllAtivas(conexao, null);

                            SQLiteTransaction transaction = conexao.BeginTransaction();

                            try
                            {
                                dadosCliente.Cliente.Id = 0;
                                dadosCliente.Cliente.Salvar(true, conexao, transaction);
                                dadosCliente.Tatuagem.IdCliente = dadosCliente.Cliente.Id;
                                dadosCliente.Tatuagem.Salvar(conexao, transaction);
                                dadosCliente.Sessao.IdTatuagem = dadosCliente.Tatuagem.Id;
                                dadosCliente.Sessao.Salvar(conexao, transaction);

                                foreach (Resposta resposta in dadosCliente.Respostas)
                                {
                                    if (perguntas.Where(pergunta => pergunta.Id == resposta.IdPergunta).ToArray()[0].Tipo == TipoPergunta.Cliente)
                                    {
                                        resposta.IdReferencia = dadosCliente.Cliente.Id;
                                    }
                                    else
                                    {
                                        resposta.IdReferencia = dadosCliente.Tatuagem.Id;
                                    }

                                    resposta.Salvar(conexao, transaction);
                                }

                                transaction.Commit();

                                worksheet.Cells[i, colCount + 1].Value = "Importado com sucesso!";
                            }
                            catch (Exception erro)
                            {
                                worksheet.Cells[i, colCount + 1].Value = erro.Message;
                                transaction.Rollback();
                            }
                        }

                        if (qtdeIgnorados == 0)
                        {
                            MessageBox.Show("Importação finalizada com sucesso!", "Sucesso", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                        else
                        {
                            string mensagem = "Existem linhas com problemas e por isso não foram importadas, deseja salvar o arquivo com as inconsistências?";

                            if (MessageBox.Show(mensagem, "Aviso", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.Yes)
                            {
                                using (var sfdArquivo = new SaveFileDialog())
                                {
                                    sfdArquivo.Filter = "Arquivo Excel|*.xlsx";

                                    if (sfdArquivo.ShowDialog() == DialogResult.OK)
                                    {
                                        if (File.Exists(sfdArquivo.FileName))
                                        {
                                            File.Delete(sfdArquivo.FileName);
                                        }

                                        arquivo.SaveAs(new FileInfo(sfdArquivo.FileName));
                                    }
                                }
                            }
                        }
                    }

            Close();
        }
示例#2
0
        private void btnModeloImportacao_Click(object sender, EventArgs e)
        {
            string fileName = String.Empty;

            using (var saveFileDialog = new SaveFileDialog())
            {
                saveFileDialog.Filter     = "Arquivo Ecxel|*.xls";
                saveFileDialog.DefaultExt = "xls";
                if (saveFileDialog.ShowDialog() != DialogResult.OK)
                {
                    return;
                }

                fileName = saveFileDialog.FileName;
            }

            using (var file = new ExcelPackage())
                using (var sheet = file.Workbook.Worksheets.Add("Modelo"))
                {
                    sheet.Cells[1, 1].Value  = "valor_pago";
                    sheet.Cells[1, 2].Value  = "data_primeira_visita";
                    sheet.Cells[1, 3].Value  = "data_ultima_visita";
                    sheet.Cells[1, 4].Value  = "nome";
                    sheet.Cells[1, 5].Value  = "sobrenome";
                    sheet.Cells[1, 6].Value  = "data_nascimento";
                    sheet.Cells[1, 7].Value  = "cpf";
                    sheet.Cells[1, 8].Value  = "email";
                    sheet.Cells[1, 9].Value  = "ddd_telefone";
                    sheet.Cells[1, 10].Value = "telefone";
                    sheet.Cells[1, 11].Value = "ddd_celular";
                    sheet.Cells[1, 12].Value = "celular";
                    sheet.Cells[1, 13].Value = "cep";
                    sheet.Cells[1, 14].Value = "tipo_logradouro";
                    sheet.Cells[1, 15].Value = "logradouro";
                    sheet.Cells[1, 16].Value = "numero";
                    sheet.Cells[1, 17].Value = "complemento";
                    sheet.Cells[1, 18].Value = "bairro";
                    sheet.Cells[1, 19].Value = "cidade";
                    sheet.Cells[1, 20].Value = "estado";
                    sheet.Cells[1, 21].Value = "area_corpo";
                    sheet.Cells[1, 22].Value = "parametros";
                    sheet.Cells[1, 23].Value = "quantidade_disparos";

                    using (var conexao = new Connection(Database.Local))
                    {
                        List <Pergunta> perguntas = Pergunta.GetAllAtivas(conexao, null);

                        int i = 0;
                        foreach (Pergunta pergunta in perguntas)
                        {
                            sheet.Cells[1, 24 + i].Value = pergunta.CodigoImportacao;
                            i++;
                        }
                    }

                    sheet.Cells[sheet.Dimension.Address].AutoFitColumns();
                    sheet.Cells[sheet.Dimension.Address].Style.HorizontalAlignment = OfficeOpenXml.Style.ExcelHorizontalAlignment.Center;

                    file.SaveAs(new FileInfo(fileName));
                }

            MessageBox.Show("Arquivo salvo com sucesso no local selecionado", "Sucesso", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }