CLR: .Net / Mono / Boo / Otros CLR > C#
Exportar Datagrid A Microsoft Excel
(1/1)
Joe Potter:
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:
sergiotarrillo:
Holas!
Acá hay un artículo: http://www.codeproject.com/csharp/export.asp.
Este artículo usa Referencias (dll) de Excel, para lograr su cometido.
Saludos,
jonathan_242:
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# ---public void exporta_a_excel() { Microsoft.Office.Interop.Excel.ApplicationClass excel = new ApplicationClass(); excel.Application.Workbooks.Add(true); int ColumnIndex = 0; foreach (DataGridViewColumn col in TuDataGrid.Columns ) { ColumnIndex++; excel.Cells[1, ColumnIndex] = col.Name; } int rowIndex = 0; foreach (DataGridViewRow row in TuDataGrid.Rows ) { rowIndex++; ColumnIndex = 0; foreach (DataGridViewColumn col in TuDataGrid.Columns) { ColumnIndex++; excel.Cells[rowIndex + 1, ColumnIndex] = row.Cells[col.Name].Value ; } } excel.Visible = true; Worksheet worksheet = (Worksheet)excel.ActiveSheet; worksheet.Activate(); }
Navegación
Ir a la versión completa