tmndungu
tmndungu

Reputation: 358

Exporting a hyperlink field to Excel not showing

I have a button that displays data on the grid in asp.net and another button with c# code behind it that export the same fields into excel. The grid display data source looks as follows:

<Columns>
    <asp:BoundField HeaderText="Location" DataField="Location" SortExpression="Location" /> 
    <asp:BoundField HeaderText="Latitude" DataField="GPSLatitude" SortExpression="GPSLatitude" /> 
    <asp:BoundField HeaderText="Latitude" DataField="GPSLatitude" SortExpression="GPSLatitude" /> 
    <asp:BoundField HeaderText="Picture Link" DataField="PicLink" **Visible="true"**/>  
</Columns>

When I add an attribute Visible="true" to the PicLink, which is a hyperlink eg http://beta.example.co.za/trials/Pictures/V_2992.jpg, the field will display on the data grid view and also displays the Excel export. If I set Visible="false",both the data grid and Excel export don't display the field. The problem is I only want to display the field in Excel export but not on the data grid.

The C# code behind the 'Export to Excel' button looks as follows:

string attachment = "attachment; filename=ArchivedStuff.xls";

Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);

gvLogDetails.AllowPaging = false;
gvLogDetails.AllowSorting = false;
string sort = ViewState["LogSortExpression"].ToString() + ' ' + ViewState["LogSortDirection"];
loadGrid(sort);

GridView grdExport = new GridView();
grdExport.AllowPaging = false;
grdExport.AllowSorting = false;
grdExport.AutoGenerateColumns = false;
grdExport = gvLogDetails;

int colcount = grdExport.Columns.Count;
for (int i = 1; i < colcount; i++)
{
    grdExport.Columns[i].SortExpression = "";
}

grdExport.Columns[0].Visible = false;
grdExport.Columns[16].Visible = true; //the PicLink field

//Prepare the grid for exporting to excell
ExportToExcel.PrepareGridViewForExport(grdExport);

// Create a form to contain the grid
HtmlForm frm = new HtmlForm();

gvLogDetails.Parent.Controls.Add(frm);
frm.Attributes["runat"] = "server";
frm.Controls.Add(grdExport);
frm.RenderControl(htw);
Response.Write(sw.ToString());

Response.End();

//ExportToExcel

public class ExportToExcel
{
    #region Export to Excel
    public static void PrepareGridViewForExport(Control gv)
    {
        Literal l = new Literal();

        for (int i = 0; i < gv.Controls.Count; i++)
        {
            if (gv.Controls[i].GetType() == typeof(LinkButton))
            {
                l.Text = (gv.Controls[i] as LinkButton).Text;

                gv.Controls.Remove(gv.Controls[i]);
                gv.Controls.AddAt(i, l);
            }
            else if (gv.Controls[i].GetType() == typeof(HyperLink))
            {
                l.Text = (gv.Controls[i] as HyperLink).Text;

                gv.Controls.Remove(gv.Controls[i]);
                gv.Controls.AddAt(i, l);
            }
            else if (gv.Controls[i].GetType() == typeof(DropDownList))
            {
                l.Text = (gv.Controls[i] as DropDownList).SelectedItem.Text;

                gv.Controls.Remove(gv.Controls[i]);
                gv.Controls.AddAt(i, l);
            }
            else if (gv.Controls[i].GetType() == typeof(CheckBox))
            {
                l.Text = (gv.Controls[i] as CheckBox).Checked ? "True" : "False";

                gv.Controls.Remove(gv.Controls[i]);
                gv.Controls.AddAt(i, l);
            }
            else if (gv.Controls[i].GetType().Name == "DataControlLinkButton")
            {
                l.Text = (gv.Controls[i] as LinkButton).Text;

                gv.Controls.Remove(gv.Controls[i]);
                gv.Controls.AddAt(i, l);
            }

            if (gv.Controls[i].HasControls())
            {
                PrepareGridViewForExport(gv.Controls[i]);
            }
        }
    }
    #endregion
}

Upvotes: 1

Views: 1301

Answers (1)

Emanuele
Emanuele

Reputation: 664

Only in the Excel export:

In gvLogDetails (I suppose in aspx) add

Visible="false"

on the 16th column. You can remove

grdExport.AutoGenerateColumns = false;

and you have to rebind data.

You can also use gvLogDetails directly.

Upvotes: 1

Related Questions