private void btnImportar_Click(object sender, EventArgs e) { ILog Logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); Stream myStream = null; OpenFileDialog openFileDialog2 = new OpenFileDialog(); lblFechaInicioText.Text = ""; lblFechaFinText.Text = ""; lblResultado.Text = ""; openFileDialog2.InitialDirectory = "D:\\"; openFileDialog2.Filter = "Text Files(.txt)|*.txt"; openFileDialog2.FilterIndex = 2; openFileDialog2.RestoreDirectory = true; if (openFileDialog2.ShowDialog() == DialogResult.OK) { try { btnCrearDE.Enabled = false; string mensaje = string.Empty; string keystring = "DEMO PARA IMPORTAR"; if ((myStream = openFileDialog2.OpenFile()) != null) { using (myStream) { lblFechaInicioText.Text = DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToLongTimeString(); string fullPath = openFileDialog2.FileName; if (System.IO.File.Exists(fullPath)) { DataExtensionHelper dataExtensionHelper = new DataExtensionHelper(); mensaje = dataExtensionHelper.Importar(keystring, fullPath); } lblFechaFinText.Text = DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToLongTimeString(); lblResultado.Text = mensaje; } } btnCrearDE.Enabled = true; } catch (Exception ex) { lblResultado.Text = "Hubo un error en la generación de DataExtension."; Logger.Info(ex.Message); } } }
private void btnCrearDE_Click(object sender, EventArgs e) { ILog Logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); Stream myStream = null; OpenFileDialog openFileDialog1 = new OpenFileDialog(); lblFechaInicioText.Text = ""; lblFechaFinText.Text = ""; lblResultado.Text = ""; openFileDialog1.InitialDirectory = "D:\\"; openFileDialog1.Filter = "Excel Files(.xls)|*.xls"; openFileDialog1.FilterIndex = 2; openFileDialog1.RestoreDirectory = true; if (openFileDialog1.ShowDialog() == DialogResult.OK) { try { btnCrearDE.Enabled = false; string mensaje = string.Empty; if ((myStream = openFileDialog1.OpenFile()) != null) { using (myStream) { lblFechaInicioText.Text = DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToLongTimeString(); string fullPath = openFileDialog1.FileName; if (System.IO.File.Exists(fullPath)) { using (var fileStreamUpload = new FileStream(fullPath, FileMode.Open, FileAccess.Read)) { var workbook = new HSSFWorkbook(fileStreamUpload); ISheet sheet = workbook.GetSheet("DataExtension"); bool result = false; DataExtensionHelper dataExtensionHelper = new DataExtensionHelper(); DataExtensionHead dataExtensionHead = new DataExtensionHead(); dataExtensionHead.Nombre = sheet.GetRow(1).GetCell(1).ToString(); dataExtensionHead.Descripcion = sheet.GetRow(2).GetCell(1).ToString(); int usadoParaEnviar; result = Int32.TryParse(sheet.GetRow(3).GetCell(1).ToString(), out usadoParaEnviar); if (!result) { throw new Exception("El valor de Usado para enviar es inválido"); } else { dataExtensionHead.EsUsadaComoBase = (usadoParaEnviar == (int)TipoSiNo.Si); } if (dataExtensionHead.EsUsadaComoBase) { dataExtensionHead.NombreBaseFuente = sheet.GetRow(3).GetCell(3).ToString(); dataExtensionHead.NombreBaseMostrar = sheet.GetRow(3).GetCell(5).ToString(); } int rowNumber = 6; while (sheet.GetRow(rowNumber)!= null) { DataExtensionDetalle dataExtensionDetalle = new DataExtensionDetalle(); string nombreCampo = sheet.GetRow(rowNumber).GetCell(0).ToString(); dataExtensionDetalle.Campo = nombreCampo; int tipoDato; result = Int32.TryParse(sheet.GetRow(rowNumber).GetCell(1).ToString(), out tipoDato); if (!result) { throw new Exception("El Tipo de Dato del campo " + nombreCampo + " es inválido"); } else { dataExtensionDetalle.Tipo = tipoDato; } int tamanio; result = Int32.TryParse(sheet.GetRow(rowNumber).GetCell(2).ToString(), out tamanio); if (!result) { throw new Exception("El Tamaño del campo " + nombreCampo + " es inválido"); } else { dataExtensionDetalle.Tamanio = tamanio; } int clavePrimaria; result = Int32.TryParse(sheet.GetRow(rowNumber).GetCell(3).ToString(), out clavePrimaria); if (!result) { throw new Exception("El valor ingresado en Clave Primaria del campo " + nombreCampo + " es inválido"); } else { dataExtensionDetalle.EsClavePrimaria = (clavePrimaria == (int)TipoSiNo.Si); } int campoNulo; result = Int32.TryParse(sheet.GetRow(rowNumber).GetCell(4).ToString(), out campoNulo); if (!result) { throw new Exception("El valor ingresado en Campo Nulo del campo " + nombreCampo + " es inválido"); } else { dataExtensionDetalle.EsNulo = (campoNulo == (int)TipoSiNo.Si); } dataExtensionDetalle.ValorPorDefecto = sheet.GetRow(rowNumber).GetCell(5).ToString(); dataExtensionHead.DataExtensionDetalleList.Add(dataExtensionDetalle); rowNumber++; } mensaje = dataExtensionHelper.Crear(dataExtensionHead); } } lblFechaFinText.Text = DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToLongTimeString(); lblResultado.Text = mensaje; } } btnCrearDE.Enabled = true; } catch (Exception ex) { lblResultado.Text = "Hubo un error en la generación de DataExtension."; Logger.Info(ex.Message); } } }
private void btnImportar_Click(object sender, EventArgs e) { DataExtensionHelper dataExtensionHelper = new DataExtensionHelper(); var response = dataExtensionHelper.Importar("DataExtension1List1_20150924163300", @"DataExtension1List1.txt"); MessageBox.Show(response, "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Warning); }