Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Lalo

« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS 

Read and import an Excel Workbook with CSharp ver1

// All this code should belong to a class

   1  
   2  //Variables privadas para controlar Excel
   3  private Excel.Application objExcel =  null;
   4  private Excel.Workbook objWorkbook = null;
   5  private Excel.Worksheet objWorkSheet = null;
   6  
   7  private void RecorrerArchivoExcel(string strArchivo, int intHoja)
   8  {
   9          if (this.InicializarExcel())
  10          {
  11                  this.AbrirExcelWorkBook(strArchivo, intHoja);
  12  
  13                  for (int i = 1; i <= 10; i++)
  14                  {
  15                          string tmp = (string) objWorkSheet.get_Range("J"+i.ToString(), Missing.Value ).Text;
  16                          this.lstContenido.Items.Add(tmp);
  17                  }
  18  
  19                  //Cerrar el archivo
  20                  objWorkbook.Close(false,null,null);
  21          }
  22  
  23  }
  24  
  25  
  26  private bool AbrirExcelWorkBook(string strArchivo, int intHoja)
  27  {
  28          try
  29          {
  30                  //Abrir el workbook
  31                  objWorkbook = objExcel.Workbooks.Open(strArchivo, 0, true, 5,
  32                          "", "", true, Excel.XlPlatform.xlWindows, "\t", false, false,
  33                          0, true,null,null);
  34  
  35                  // Obtener la coleccion de hojas del workbook
  36                  Excel.Sheets sheets = objWorkbook.Worksheets;
  37  
  38                  // Obtener la hoja necesaria
  39                  objWorkSheet = (Excel.Worksheet) sheets.get_Item(intHoja);
  40                 
  41                  //Devolver el control
  42                  return true;
  43          }
  44          catch (Exception ex)
  45          {
  46                  MessageBox.Show(ex.Message);
  47                  return false;
  48          }
  49  }
  50  
  51  private bool InicializarExcel()
  52  {
  53          try
  54          {
  55                  objExcel = new Excel.Application();
  56  
  57                  //  Chekear si el objeto excel pudo ser creado
  58                  if (objExcel == null)
  59                  {
  60                          MessageBox.Show("ERROR: No se pudo ejecutar Microsoft EXCEL");
  61                          return false;
  62                  }
  63  
  64                  //  Visualizar el objeto excel
  65                  objExcel.Visible = false;
  66  
  67                  //Se logro inicializar el componente Excel
  68                  return true;
  69          }
  70          catch (Exception ex)
  71          {
  72                  MessageBox.Show(ex.Message);
  73                  return false;
  74          }
  75  }

Populating a Winforms ComboBox with csharp

   1  
   2  private void PoblarComboPais()
   3  {
   4  	// Trae las sociedades correspondientes al país
   5  	Cursor.Current = Cursors.WaitCursor;
   6  	// Conexión
   7  	string cn = SqlOptions.ConnectionString();
   8  
   9  	// DataReader
  10  	this.cboPais.DataSource = SqlHelper.ExecuteDataset(cn,CommandType.Text , "SELECT * FROM MAE_PAIS").Tables[0];
  11  	this.cboPais.ValueMember = "chrPais";
  12  	this.cboPais.DisplayMember = "chrPaisNombre";
  13  
  14  	//Puntero del mouse normal
  15  	Cursor.Current = Cursors.Default;
  16  }
« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS