• Jueves 2 de Mayo de 2024, 03:50

Autor Tema:  Generar un archivo excel  (Leído 2473 veces)

cachorro

  • Miembro MUY activo
  • ***
  • Mensajes: 196
  • Nacionalidad: ec
    • Ver Perfil
    • http://www.pumamix.com
Generar un archivo excel
« en: Martes 16 de Noviembre de 2010, 14:24 »
0
Saludos a todos, alguien sabe como se puede generar un archivo de excel con los datos que yo de pase o que tome desde una grilla, me urge hacer esto espero alguiem me pueda ayudar...
La Vida es un Reto..Enfrentala....

edwin_orlando83

  • Nuevo Miembro
  • *
  • Mensajes: 14
    • Ver Perfil
Re: Generar un archivo excel
« Respuesta #1 en: Martes 16 de Noviembre de 2010, 16:03 »
0
yo hice una clase pero no esta muy estructurada

using System;
using System.Drawing;
namespace Utilidades
{
   /// <summary>
   /// Description of C_EXCEL.
   /// </summary>
   public class C_EXCEL
   {
      public C_EXCEL()
      {
         
      }      
      public void PasarAexcel(System.Windows.Forms.DataGridView dataGridView1, string mensaje )
      {
      
        
         Excel.Application oXL;
            Excel._Workbook oWB;
            Excel._Worksheet oSheet;
            Excel.Range oRng;
           
            try
            {
               if( dataGridView1.Rows.Count > 0)
               {
                oXL = new Excel.Application();
                oXL.Visible = false;                
                oWB = (Excel._Workbook)(oXL.Workbooks.Add(Type.Missing));              
                oSheet = (Excel._Worksheet)oWB.ActiveSheet;
                oSheet.get_Range("A3", "D3").Font.Bold = true;               
                oSheet.Cells[3, 2] = mensaje;            
                int jv = 1;              
               foreach (System.Windows.Forms.DataGridViewColumn c in dataGridView1.Columns)
                {                  
                      if( c.Visible)
                      {                        
                         oSheet.Cells[4, jv] = c.HeaderText;
                         int fila = 5;
                           foreach( System.Windows.Forms.DataGridViewRow r in dataGridView1.Rows)
                           {
                              oSheet.Cells[fila++, jv] = r.Cells[c.Name].Value;                               
                           }                         
                         jv++;
                           
                      }

                }
                jv=1;
                oRng = oSheet.get_Range("I1", "N1");
              oRng.EntireColumn.AutoFit();         

              System.Windows.Forms.MessageBox.Show("Se Exporto a Excel sin Problemas.nEl archivo se abrira automaticamente");                
              oXL.Visible = true;
                oXL.UserControl = true;                
               
               }
               else
                  System.Windows.Forms.MessageBox.Show("No hay datos para exportar a Excel","Error");
               
                }
            catch (Exception theException)
            {
                String errorMessage;
                errorMessage = "Error: ";
                errorMessage = String.Concat(errorMessage, theException.Message);
                errorMessage = String.Concat(errorMessage, " Line: ");
                errorMessage = String.Concat(errorMessage, theException.Source);
                System.Windows.Forms.MessageBox.Show(errorMessage, "Error");
            }
      
      }
      
   }
}

utilizo las librerias del office 2003

cachorro

  • Miembro MUY activo
  • ***
  • Mensajes: 196
  • Nacionalidad: ec
    • Ver Perfil
    • http://www.pumamix.com
Re: Generar un archivo excel
« Respuesta #2 en: Martes 16 de Noviembre de 2010, 19:13 »
0
gracias lo voy a probar, pero hay que habilitar algun using o solo con ese codigo funciona..gracias de antemano..
La Vida es un Reto..Enfrentala....

eversm

  • Nuevo Miembro
  • *
  • Mensajes: 11
    • Ver Perfil
Re: Generar un archivo excel
« Respuesta #3 en: Jueves 18 de Noviembre de 2010, 06:03 »
0
me parece que tmb se puede hacer con conexion ODBC al excel, me encontre este codigo en la web

 
Código: C#
  1.  
  2.  
  3. String sConnectionString = "Provider=MSDASQL;Driver={Microsoft Excel
  4. Driver(*.xls)};DBQ=c:\temp\Sale_Test.xls;;;Exten ded
  5. Properties=Excel10.0;HDR=No;";
  6.  
  7. // Create connection object by using the preceding connection string.
  8.  
  9. objConn = new OdbcConnection(sConnectionString);
  10.  
  11. OdbcCommand objCmdSelect =new OdbcCommand();
  12.  
  13.  
  14.  
  15. objCmdSelect.Connection = objConn;
  16.  
  17. objCmdSelect.CommandText = "Select * From [Week1$]";
  18.  
  19. objConn.Open();
  20.  
  21.  
  22.  

igual y te sirve