SoloCodigo

CLR: .Net / Mono / Boo / Otros CLR => C# => Mensaje iniciado por: Joe Potter en Jueves 6 de Julio de 2006, 00:44

Título: Exportar Datagrid A Microsoft Excel
Publicado por: Joe Potter en Jueves 6 de Julio de 2006, 00:44
Por favor si alguien puede ayudarme  <_< a exportar un datagrid a excel con Visual C#.net se los agradeceria mucho.

Es que ya he buscado muchos ejemplos pero solo he encontrado  en visual basic .net .

Ademas estoy usando Visual Studio .net 2002, nose si se pueda con esta version exportar a excel.

Si alguien puede contestarme algo se lo agradecer muchisimo jejejej. :P

Saludos cordiales a todos !!!!! :D  :hola:

*****************************************************************
--> De hecho este es el codigo de la forma donde no funciona la exportacion si alguien sabe como arreglarlo o mejorar diganmelo. ESTA REALIZADO EN "VISUAL STUDIO 2002, VISUAL C#.NET."

--> estas son las librerias que se usan, segun  esto.
*************************************
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using CoreLab.MySql;
using RKLib.ExportData;
**********************************
--> este es el codigo para el proceso de exportacion con un boton.


   private void Form1_Load(object sender, System.EventArgs e)
      {
         this.llenaGrid();

      }
      

   private void button1_Click(object sender, System.EventArgs e)
      {
         this.Close();
      }
      
      private void llenaGrid()
      {
         string sConnectionString;
         sConnectionString = "Host=localhost;User Id=amor;Database=ejemplo1;";

         MySqlConnection con1 = new MySqlConnection(sConnectionString);
         con1.Open();

         MySqlDataAdapter daPrueba = new MySqlDataAdapter("SELECT * FROM alumno", con1);
         DataSet dsPrueba = new DataSet("alumno");
         
         daPrueba.FillSchema(dsPrueba,SchemaType.Source, "alumno");
         daPrueba.Fill(dsPrueba,"alumno");

         DataTable tbldbo_Prueba;
         tbldbo_Prueba = dsPrueba.Tables["alumno"];
         dataGrid1.DataSource = tbldbo_Prueba;
         //return dsPrueba;

      }

      

      private void btnExportar_Click_1(object sender, System.EventArgs e)
      {
         DataTable tbldbo_Prueba = dsPrueba.Tables["alumno"].Copy();

         // Export all the details to Excel
         RKLib.ExportData.Export objExport = new RKLib.ExportData.Export("Win");            
         objExport.ExportDetails(tbldbo_Prueba, Export.ExportFormat.Excel, "\\alumnoinfo.xls");
         MessageBox.Show("Informacion Exportada");

      }

   }
}
*******************************************************************
 :hola:
Título: Re: Exportar Datagrid A Microsoft Excel
Publicado por: sergiotarrillo en Viernes 4 de Agosto de 2006, 02:15
Holas!

Acá hay un artículo: http://www.codeproject.com/csharp/export.asp (http://www.codeproject.com/csharp/export.asp).

Este artículo usa Referencias (dll) de Excel, para lograr su cometido.

Saludos,
Título: Re: Exportar Datagrid A Microsoft Excel
Publicado por: jonathan_242 en Miércoles 24 de Noviembre de 2010, 17:30
esta funcion exporta el contenido de un DataGridView a Excel usando C#
para esto tiene que agregar la referencia "Microsoft Excel 12.0 Object Library"
que se encuentra en la pestaña de COM

Código: C#
  1. public void exporta_a_excel()
  2.         {
  3.             Microsoft.Office.Interop.Excel.ApplicationClass excel = new ApplicationClass();
  4.             excel.Application.Workbooks.Add(true);
  5.             int ColumnIndex = 0;
  6.             foreach (DataGridViewColumn col in TuDataGrid.Columns  )
  7.             {
  8.                 ColumnIndex++;
  9.                 excel.Cells[1, ColumnIndex] = col.Name;
  10.             }
  11.             int rowIndex = 0;
  12.             foreach (DataGridViewRow row in TuDataGrid.Rows )
  13.             {
  14.                 rowIndex++;
  15.                 ColumnIndex = 0;
  16.                 foreach (DataGridViewColumn col in TuDataGrid.Columns)
  17.                 {
  18.                     ColumnIndex++;
  19.                     excel.Cells[rowIndex + 1, ColumnIndex] = row.Cells[col.Name].Value ;
  20.                 }
  21.             }
  22.             excel.Visible = true;
  23.             Worksheet worksheet = (Worksheet)excel.ActiveSheet;
  24.             worksheet.Activate();
  25.         }
  26.