<?xml version="1.0" encoding="utf-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comentarios en: Exportar el contenido de un GridView a Excel</title>
	<atom:link href="http://www.buayacorp.com/archivos/exportar-el-contenido-de-un-gridview-a-excel/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.buayacorp.com/archivos/exportar-el-contenido-de-un-gridview-a-excel/</link>
	<description>Diseño y Programación</description>
	<lastBuildDate>Tue, 07 Feb 2012 11:50:05 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
	<item>
		<title>Por: Antoni Nerin Toboso</title>
		<link>http://www.buayacorp.com/archivos/exportar-el-contenido-de-un-gridview-a-excel/#comment-44146</link>
		<dc:creator>Antoni Nerin Toboso</dc:creator>
		<pubDate>Tue, 07 Feb 2012 11:50:05 +0000</pubDate>
		<guid isPermaLink="false">http://www.buayacorp.com/archivos/exportar-el-contenido-de-un-gridview-a-excel/#comment-44146</guid>
		<description>Esta es una clase en VB que genera un excel a partir de un DataSet.

Imports System
Imports System.Collections.Generic
Imports System.Text
Imports System.Drawing

Imports Microsoft.Office.Interop.Excel
Imports System.Reflection

Imports config = System.Configuration
Imports ficheros = System.IO
Imports Variables.cVariables

Namespace Exportar
    Public Class ExportarDades

        Public Sub New()
        End Sub

        Public Shared Sub Exportar_a_Excel(ByVal Titol As String, ByVal ExcelName As String, ByVal sheets As String, ByVal DS As DataSet)

            &#039; Prevenir conflicto de idiomas. Si no se pone genera este error:
            &#039;              Old format or invalid type library. (Exception from HRESULT: 0x80028018 (TYPE_E_INVDATAREAD))
            System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture(&quot;en-US&quot;)

            Try

                Dim _excel As New Application()
                Dim _wBook As Workbook = _excel.Workbooks.Add(Missing.Value)

                Dim idx As Integer = 0
                While idx &lt; DS.Tables.Count

                    Dim _sheet As Worksheet = DirectCast(_wBook.Worksheets.Add(Missing.Value, Missing.Value, Missing.Value, Missing.Value), Worksheet)
                    _sheet.Name = sheets

                    &#039; Situación del logo —si está en—: A1
                    Dim imagen As Object = _sheet.Pictures.Insert(&quot;logo.jpg&quot;)
                    Dim cell = _sheet.Cells(1, 1)

                    &#039;Centramos el ancho
                    Dim ancho As Double = cell.Offset(0, 1).Left - cell.Left
                    imagen.Left = cell.Left + ancho / 2 - imagen.Width / 2
                    If imagen.Left &lt; 1 Then imagen.Left = 1
                    &#039;Centramos el alto
                    Dim alto As Double = cell.Offset(1, 0).Top - cell.Top
                    imagen.Top = cell.Top + alto / 2 - imagen.Height / 2
                    If imagen.Top &lt; 1 Then imagen.Top = 1

                    &#039;Suponemos que el logo ocupa 5 filas..., montamos el título del informe en la fila 6 i damos el format establecido. 
                    Dim r As Integer = 6
                    _sheet.Cells(r, 1) = Titol.ToString
                    Dim rng As Range = DirectCast(_sheet.Cells(r, 1), Range)
                    rng.EntireRow.Font.Bold = True
                    rng.EntireRow.Font.Size = 20
                    rng.EntireRow.Interior.ColorIndex = 40
                    rng.EntireRow.Font.ColorIndex = 30

                    &#039;2 lineas mes i montamos las cabeceras de las columnes i damos el format
                    r += 2
                    Dim k = 0
                    While k &lt; DS.Tables(idx).Columns.Count
                        _sheet.Cells(r, k + 1) = DS.Tables(idx).Columns(k).ColumnName.ToString()
                        System.Math.Max(System.Threading.Interlocked.Increment(k), k - 1)
                    End While
                    rng = DirectCast(_sheet.Cells(r, DS.Tables(idx).Columns.Count), Range)
                    rng.EntireRow.Font.Bold = True
                    rng.EntireRow.Interior.ColorIndex = 30
                    rng.EntireRow.Font.ColorIndex = 40

                    &#039;Y a partir de aquí, montamos todos los datos del DataSet
                    r = 0
                    While r &lt; DS.Tables(idx).Rows.Count
                        k = 0
                        While k &lt; DS.Tables(idx).Columns.Count
                            _sheet.Cells(r + 9, k + 1) = DS.Tables(idx).Rows(r).ItemArray(k)
                            System.Math.Max(System.Threading.Interlocked.Increment(k), k - 1)
                        End While
                        System.Math.Max(System.Threading.Interlocked.Increment(r), r - 1)

                    End While
                    System.Math.Max(System.Threading.Interlocked.Increment(idx), idx - 1)
                End While

                If ficheros.File.Exists(ExcelName) Then
                    ficheros.File.Delete(ExcelName)
                End If

                &#039; Salimos del Excel 
                _excel.ActiveCell.Worksheet.SaveAs(ExcelName, XlFileFormat.xlOpenXMLWorkbook, Missing.Value, Missing.Value, Missing.Value, Missing.Value, _
                 Missing.Value, Missing.Value, Missing.Value, Missing.Value)
                _excel.Quit()

                &#039; Mostrar el excel
                _excel.Visible = False

                &#039; Matamos el proces si queda abierto
                deleteProcess()
            Catch ex As Exception
                Dim ss As String = ex.Message

                &#039; Matamos el proces si queda abierto
                deleteProcess(&quot;Excel&quot;)
            End Try

        End Sub

        Private Shared Sub deleteProcess(ByVal Process As String)

            Dim miproceso As System.Diagnostics.Process() = System.Diagnostics.Process.GetProcessesByName(Processo)

            For Each pc As System.Diagnostics.Process In miproceso
                pc.Kill()
            Next

        End Sub

    End Class
End Namespace</description>
		<content:encoded><![CDATA[<p>Esta es una clase en VB que genera un excel a partir de un DataSet.</p>
<p>Imports System<br />
Imports System.Collections.Generic<br />
Imports System.Text<br />
Imports System.Drawing</p>
<p>Imports Microsoft.Office.Interop.Excel<br />
Imports System.Reflection</p>
<p>Imports config = System.Configuration<br />
Imports ficheros = System.IO<br />
Imports Variables.cVariables</p>
<p>Namespace Exportar<br />
    Public Class ExportarDades</p>
<p>        Public Sub New()<br />
        End Sub</p>
<p>        Public Shared Sub Exportar_a_Excel(ByVal Titol As String, ByVal ExcelName As String, ByVal sheets As String, ByVal DS As DataSet)</p>
<p>            ' Prevenir conflicto de idiomas. Si no se pone genera este error:<br />
            '              Old format or invalid type library. (Exception from HRESULT: 0x80028018 (TYPE_E_INVDATAREAD))<br />
            System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture("en-US")</p>
<p>            Try</p>
<p>                Dim _excel As New Application()<br />
                Dim _wBook As Workbook = _excel.Workbooks.Add(Missing.Value)</p>
<p>                Dim idx As Integer = 0<br />
                While idx &lt; DS.Tables.Count</p>
<p>                    Dim _sheet As Worksheet = DirectCast(_wBook.Worksheets.Add(Missing.Value, Missing.Value, Missing.Value, Missing.Value), Worksheet)<br />
                    _sheet.Name = sheets</p>
<p>                    &#039; Situación del logo —si está en—: A1<br />
                    Dim imagen As Object = _sheet.Pictures.Insert(&quot;logo.jpg&quot;)<br />
                    Dim cell = _sheet.Cells(1, 1)</p>
<p>                    &#039;Centramos el ancho<br />
                    Dim ancho As Double = cell.Offset(0, 1).Left - cell.Left<br />
                    imagen.Left = cell.Left + ancho / 2 - imagen.Width / 2<br />
                    If imagen.Left &lt; 1 Then imagen.Left = 1<br />
                    &#039;Centramos el alto<br />
                    Dim alto As Double = cell.Offset(1, 0).Top - cell.Top<br />
                    imagen.Top = cell.Top + alto / 2 - imagen.Height / 2<br />
                    If imagen.Top &lt; 1 Then imagen.Top = 1</p>
<p>                    &#039;Suponemos que el logo ocupa 5 filas..., montamos el título del informe en la fila 6 i damos el format establecido.<br />
                    Dim r As Integer = 6<br />
                    _sheet.Cells(r, 1) = Titol.ToString<br />
                    Dim rng As Range = DirectCast(_sheet.Cells(r, 1), Range)<br />
                    rng.EntireRow.Font.Bold = True<br />
                    rng.EntireRow.Font.Size = 20<br />
                    rng.EntireRow.Interior.ColorIndex = 40<br />
                    rng.EntireRow.Font.ColorIndex = 30</p>
<p>                    &#039;2 lineas mes i montamos las cabeceras de las columnes i damos el format<br />
                    r += 2<br />
                    Dim k = 0<br />
                    While k &lt; DS.Tables(idx).Columns.Count<br />
                        _sheet.Cells(r, k + 1) = DS.Tables(idx).Columns(k).ColumnName.ToString()<br />
                        System.Math.Max(System.Threading.Interlocked.Increment(k), k - 1)<br />
                    End While<br />
                    rng = DirectCast(_sheet.Cells(r, DS.Tables(idx).Columns.Count), Range)<br />
                    rng.EntireRow.Font.Bold = True<br />
                    rng.EntireRow.Interior.ColorIndex = 30<br />
                    rng.EntireRow.Font.ColorIndex = 40</p>
<p>                    &#039;Y a partir de aquí, montamos todos los datos del DataSet<br />
                    r = 0<br />
                    While r &lt; DS.Tables(idx).Rows.Count<br />
                        k = 0<br />
                        While k &lt; DS.Tables(idx).Columns.Count<br />
                            _sheet.Cells(r + 9, k + 1) = DS.Tables(idx).Rows(r).ItemArray(k)<br />
                            System.Math.Max(System.Threading.Interlocked.Increment(k), k - 1)<br />
                        End While<br />
                        System.Math.Max(System.Threading.Interlocked.Increment(r), r - 1)</p>
<p>                    End While<br />
                    System.Math.Max(System.Threading.Interlocked.Increment(idx), idx - 1)<br />
                End While</p>
<p>                If ficheros.File.Exists(ExcelName) Then<br />
                    ficheros.File.Delete(ExcelName)<br />
                End If</p>
<p>                &#039; Salimos del Excel<br />
                _excel.ActiveCell.Worksheet.SaveAs(ExcelName, XlFileFormat.xlOpenXMLWorkbook, Missing.Value, Missing.Value, Missing.Value, Missing.Value, _<br />
                 Missing.Value, Missing.Value, Missing.Value, Missing.Value)<br />
                _excel.Quit()</p>
<p>                &#039; Mostrar el excel<br />
                _excel.Visible = False</p>
<p>                &#039; Matamos el proces si queda abierto<br />
                deleteProcess()<br />
            Catch ex As Exception<br />
                Dim ss As String = ex.Message</p>
<p>                &#039; Matamos el proces si queda abierto<br />
                deleteProcess(&quot;Excel&quot;)<br />
            End Try</p>
<p>        End Sub</p>
<p>        Private Shared Sub deleteProcess(ByVal Process As String)</p>
<p>            Dim miproceso As System.Diagnostics.Process() = System.Diagnostics.Process.GetProcessesByName(Processo)</p>
<p>            For Each pc As System.Diagnostics.Process In miproceso<br />
                pc.Kill()<br />
            Next</p>
<p>        End Sub</p>
<p>    End Class<br />
End Namespace</p>
]]></content:encoded>
	</item>
	<item>
		<title>Por: Carlos Marx</title>
		<link>http://www.buayacorp.com/archivos/exportar-el-contenido-de-un-gridview-a-excel/#comment-42874</link>
		<dc:creator>Carlos Marx</dc:creator>
		<pubDate>Wed, 18 Jan 2012 19:49:50 +0000</pubDate>
		<guid isPermaLink="false">http://www.buayacorp.com/archivos/exportar-el-contenido-de-un-gridview-a-excel/#comment-42874</guid>
		<description>Por que en mi pagina el nombre del archivo descargado me pone toda la ruta c_intetpub_reportes .... y no solo el nombre del archivo??</description>
		<content:encoded><![CDATA[<p>Por que en mi pagina el nombre del archivo descargado me pone toda la ruta c_intetpub_reportes .... y no solo el nombre del archivo??</p>
]]></content:encoded>
	</item>
	<item>
		<title>Por: Evelin Flores</title>
		<link>http://www.buayacorp.com/archivos/exportar-el-contenido-de-un-gridview-a-excel/#comment-34116</link>
		<dc:creator>Evelin Flores</dc:creator>
		<pubDate>Fri, 07 Oct 2011 15:25:39 +0000</pubDate>
		<guid isPermaLink="false">http://www.buayacorp.com/archivos/exportar-el-contenido-de-un-gridview-a-excel/#comment-34116</guid>
		<description>Por favor podrias enviarme un ejemplo del codigo fuente usado en el sitio web, gracias de antemano</description>
		<content:encoded><![CDATA[<p>Por favor podrias enviarme un ejemplo del codigo fuente usado en el sitio web, gracias de antemano</p>
]]></content:encoded>
	</item>
	<item>
		<title>Por: Moises Armando</title>
		<link>http://www.buayacorp.com/archivos/exportar-el-contenido-de-un-gridview-a-excel/#comment-34098</link>
		<dc:creator>Moises Armando</dc:creator>
		<pubDate>Fri, 07 Oct 2011 02:41:50 +0000</pubDate>
		<guid isPermaLink="false">http://www.buayacorp.com/archivos/exportar-el-contenido-de-un-gridview-a-excel/#comment-34098</guid>
		<description>Muchas gracias por la contribucion, me sirvio en una tarea que estoy realizando</description>
		<content:encoded><![CDATA[<p>Muchas gracias por la contribucion, me sirvio en una tarea que estoy realizando</p>
]]></content:encoded>
	</item>
	<item>
		<title>Por: rUBEN_0SCAR</title>
		<link>http://www.buayacorp.com/archivos/exportar-el-contenido-de-un-gridview-a-excel/#comment-33198</link>
		<dc:creator>rUBEN_0SCAR</dc:creator>
		<pubDate>Thu, 08 Sep 2011 22:43:07 +0000</pubDate>
		<guid isPermaLink="false">http://www.buayacorp.com/archivos/exportar-el-contenido-de-un-gridview-a-excel/#comment-33198</guid>
		<description>Buenas me funiono bien el codigo en CSHARP siguiente;
StringBuilder sb = new StringBuilder();
            StringWriter sw = new StringWriter(sb);
            HtmlTextWriter htw = new HtmlTextWriter(sw);

            Page page = new Page();
            HtmlForm form = new HtmlForm();

            gridMov.EnableViewState = false;

            // Deshabilitar la validación de eventos, sólo asp.net 2
            page.EnableEventValidation = false;

            // Realiza las inicializaciones de la instancia de la clase Page que requieran los diseñadores RAD.
            page.DesignerInitialize();

            page.Controls.Add(form);
            form.Controls.Add(gridMov);

            page.RenderControl(htw);
                    
            Response.Clear();
            Response.Buffer = true;
            Response.ContentType = &quot;application/vnd.ms-excel&quot;;
            Response.AddHeader(&quot;Content-Disposition&quot;, &quot;attachment;filename=data.xls&quot;);
            Response.Charset = &quot;UTF-8&quot;;
            Response.ContentEncoding = Encoding.Default;
            Response.Write(sb.ToString());
            Response.End();

Pero al abrir el archivo EXCEL sale disparado el cuadro del gridview como se puede dar formato o evitar eso solo que muestre los datos del gridview.......
Gracias .......</description>
		<content:encoded><![CDATA[<p>Buenas me funiono bien el codigo en CSHARP siguiente;<br />
StringBuilder sb = new StringBuilder();<br />
            StringWriter sw = new StringWriter(sb);<br />
            HtmlTextWriter htw = new HtmlTextWriter(sw);</p>
<p>            Page page = new Page();<br />
            HtmlForm form = new HtmlForm();</p>
<p>            gridMov.EnableViewState = false;</p>
<p>            // Deshabilitar la validación de eventos, sólo asp.net 2<br />
            page.EnableEventValidation = false;</p>
<p>            // Realiza las inicializaciones de la instancia de la clase Page que requieran los diseñadores RAD.<br />
            page.DesignerInitialize();</p>
<p>            page.Controls.Add(form);<br />
            form.Controls.Add(gridMov);</p>
<p>            page.RenderControl(htw);</p>
<p>            Response.Clear();<br />
            Response.Buffer = true;<br />
            Response.ContentType = "application/vnd.ms-excel";<br />
            Response.AddHeader("Content-Disposition", "attachment;filename=data.xls");<br />
            Response.Charset = "UTF-8";<br />
            Response.ContentEncoding = Encoding.Default;<br />
            Response.Write(sb.ToString());<br />
            Response.End();</p>
<p>Pero al abrir el archivo EXCEL sale disparado el cuadro del gridview como se puede dar formato o evitar eso solo que muestre los datos del gridview.......<br />
Gracias .......</p>
]]></content:encoded>
	</item>
	<item>
		<title>Por: Arturo Vera</title>
		<link>http://www.buayacorp.com/archivos/exportar-el-contenido-de-un-gridview-a-excel/#comment-31298</link>
		<dc:creator>Arturo Vera</dc:creator>
		<pubDate>Sat, 02 Jul 2011 22:10:49 +0000</pubDate>
		<guid isPermaLink="false">http://www.buayacorp.com/archivos/exportar-el-contenido-de-un-gridview-a-excel/#comment-31298</guid>
		<description>ejemplo del proyecto... gracias</description>
		<content:encoded><![CDATA[<p>ejemplo del proyecto... gracias</p>
]]></content:encoded>
	</item>
	<item>
		<title>Por: braulio</title>
		<link>http://www.buayacorp.com/archivos/exportar-el-contenido-de-un-gridview-a-excel/#comment-28878</link>
		<dc:creator>braulio</dc:creator>
		<pubDate>Thu, 25 Nov 2010 07:24:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.buayacorp.com/archivos/exportar-el-contenido-de-un-gridview-a-excel/#comment-28878</guid>
		<description>Acerca del mensaje, no tienes porqué preocuparte.</description>
		<content:encoded><![CDATA[<p>Acerca del mensaje, no tienes porqué preocuparte.</p>
]]></content:encoded>
	</item>
	<item>
		<title>Por: dave</title>
		<link>http://www.buayacorp.com/archivos/exportar-el-contenido-de-un-gridview-a-excel/#comment-28866</link>
		<dc:creator>dave</dc:creator>
		<pubDate>Tue, 23 Nov 2010 05:12:16 +0000</pubDate>
		<guid isPermaLink="false">http://www.buayacorp.com/archivos/exportar-el-contenido-de-un-gridview-a-excel/#comment-28866</guid>
		<description>hola, 
me funciono el codigo muchas gracias nada mas que me sale un error al abrir el excel &quot;El archivo que intenta abrir, &#039;data[3].xls&#039;, tiene otro formato que el especificado por la extension del archivo. Compruebe que el archivo no esta dañado y procede de un origen de confianza antes de abrirlo&quot; y es que en mi Grid tengo 2 columnas con unos iconos que tienen la funcionalidad de Editar y Borrar y cuando esas 2 columnas se exportan se ve un cuadro girs......Como puedo hacer para darle formato y no me aparezcan esas columnas

de ante mano gracias a quien conteste</description>
		<content:encoded><![CDATA[<p>hola,<br />
me funciono el codigo muchas gracias nada mas que me sale un error al abrir el excel "El archivo que intenta abrir, 'data[3].xls', tiene otro formato que el especificado por la extension del archivo. Compruebe que el archivo no esta dañado y procede de un origen de confianza antes de abrirlo" y es que en mi Grid tengo 2 columnas con unos iconos que tienen la funcionalidad de Editar y Borrar y cuando esas 2 columnas se exportan se ve un cuadro girs......Como puedo hacer para darle formato y no me aparezcan esas columnas</p>
<p>de ante mano gracias a quien conteste</p>
]]></content:encoded>
	</item>
	<item>
		<title>Por: Luisk</title>
		<link>http://www.buayacorp.com/archivos/exportar-el-contenido-de-un-gridview-a-excel/#comment-27874</link>
		<dc:creator>Luisk</dc:creator>
		<pubDate>Thu, 25 Feb 2010 13:47:31 +0000</pubDate>
		<guid isPermaLink="false">http://www.buayacorp.com/archivos/exportar-el-contenido-de-un-gridview-a-excel/#comment-27874</guid>
		<description>Leyendo el aporte que hace Alex el 27 de febrero de 2007. les cuento que sobre una de las líneas 

nombreGrid.AllowPaging = False
nombreGrid.EnableViewState = False
nombreGrid.DataSource = TuDataSource ( Esta sobre, si se deja saca error )
nombreGrid.DataBind() ( con esta es suficiente)

A todos mil gracias, excelente los aportes me ayudaron mucho. Hace tiempo esta buscando esta valiosa ayuda.</description>
		<content:encoded><![CDATA[<p>Leyendo el aporte que hace Alex el 27 de febrero de 2007. les cuento que sobre una de las líneas </p>
<p>nombreGrid.AllowPaging = False<br />
nombreGrid.EnableViewState = False<br />
nombreGrid.DataSource = TuDataSource ( Esta sobre, si se deja saca error )<br />
nombreGrid.DataBind() ( con esta es suficiente)</p>
<p>A todos mil gracias, excelente los aportes me ayudaron mucho. Hace tiempo esta buscando esta valiosa ayuda.</p>
]]></content:encoded>
	</item>
	<item>
		<title>Por: dragonrod</title>
		<link>http://www.buayacorp.com/archivos/exportar-el-contenido-de-un-gridview-a-excel/#comment-27836</link>
		<dc:creator>dragonrod</dc:creator>
		<pubDate>Mon, 22 Feb 2010 23:53:17 +0000</pubDate>
		<guid isPermaLink="false">http://www.buayacorp.com/archivos/exportar-el-contenido-de-un-gridview-a-excel/#comment-27836</guid>
		<description>Yo tengo un código para enviar el GRIDVIEW a una hoja EXCEL, mi problema es que en la pagina aspx aparecen varios GRIDVIEW y los que se muestren en la pagina de consulta quisiera exportarlos a EXCEL, gracias de antemano</description>
		<content:encoded><![CDATA[<p>Yo tengo un código para enviar el GRIDVIEW a una hoja EXCEL, mi problema es que en la pagina aspx aparecen varios GRIDVIEW y los que se muestren en la pagina de consulta quisiera exportarlos a EXCEL, gracias de antemano</p>
]]></content:encoded>
	</item>
</channel>
</rss>

