none
Export SQL to DataGridView with Images to Excel RRS feed

  • Question

  • Hello. EveryBody

    Maybe you can help me with a problem:
    I have a database with a table (Assets) with an Image field (img_asset). 
    I have a GridView control connected to the table via a SqlDataSource.  The Assets table is displayed with all fields including the image field, no problem. 
    I have a button that exports the GridView data to Excel.
    When I export to Excel, the text columns are normally displayed, but the Image column is not displayed and in the Excel file in the Image cell a red X is displayed.

    How do I display the images in the excel file?

    ASP code

            <asp:GridView ID="gv_activototal" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" BackColor="White" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" CellPadding="3" DataSourceID="SqlDataSource1" ForeColor="Black" GridLines="Vertical">
                        <AlternatingRowStyle BackColor="#CCCCCC" />
                        <Columns>
                            <asp:TemplateField HeaderText="Datos Activo">
                                <ItemTemplate>
                                    <strong>Código Activo:<br /> Departamento:</strong><br /> <strong>Marca:</strong>&nbsp;&nbsp;&nbsp;
                                    <br />
                                    <strong>Modelo:</strong><br /> <strong>Serie:</strong><br /> <strong>Bien:</strong><br /> <strong>Clase:</strong><br /> <strong>Subclase:</strong><br /> <strong>Fecha Compra:</strong><br /> <strong>Valor Compra:</strong><br />
                                </ItemTemplate>
                                <HeaderStyle Width="120px" />
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Valor">
                                <ItemTemplate>
                                    <asp:Label ID="Label11" runat="server" Text='<%# Eval("num_activo") %>'></asp:Label>
                                    <br />
                                    <asp:Label ID="Label18" runat="server" Text='<%# Eval("subclase") %>'></asp:Label>
                                    <br />
                                    <asp:Label ID="Label19" runat="server" Text='<%# Eval("fecha_compra") %>'></asp:Label>
                                    <br />
                                    <asp:Label ID="Label20" runat="server" Text='<%# Eval("valor_compra") %>'></asp:Label>
                                </ItemTemplate>
                                <HeaderStyle Width="400px" />
                            </asp:TemplateField>
                        <asp:TemplateField HeaderText="Foto">
                            <ItemTemplate>
                                <asp:Image ID="Image1" runat="server" Height="100px" ImageUrl='<%#"data:image/png;base64," + Convert.ToBase64String((byte[])Eval("foto")) %>' />                
                            </ItemTemplate>
                            <HeaderStyle HorizontalAlign="Center" />
                        </asp:TemplateField>        
                    </Columns>
                        <FooterStyle BackColor="#CCCCCC" />
                        <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
                        <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
                        <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
                        <SortedAscendingCellStyle BackColor="#F1F1F1" />
                        <SortedAscendingHeaderStyle BackColor="#808080" />
                        <SortedDescendingCellStyle BackColor="#CAC9C9" />
                        <SortedDescendingHeaderStyle BackColor="#383838" />
                    </asp:GridView>

    DATASOURCE

    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ActivexConnectionString %>" SelectCommand="SELECT * FROM [Vista_Activos]"></asp:SqlDataSource>
     

    BUTTON EXPORT

    protected void btn_excel_Click(object sender, EventArgs e)
        {
    
            Response.ClearContent();
            Response.AppendHeader("content-disposition", "attachment; filename=ActivosTotal.xls");
            Response.ContentType = "application/Excel";
    
            StringWriter stringWriter = new StringWriter();
            HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter);
    
            gv_activototal.RenderControl(htmlTextWriter);
            Response.Write(stringWriter.ToString());
            Response.End();
        }

            
    Friday, December 29, 2017 5:37 PM

All replies