【问题标题】:Exporting a hyperlink field to Excel not showing将超链接字段导出到 Excel 未显示
【发布时间】:2016-07-04 18:45:14
【问题描述】:

我有一个在 asp.net 中的网格上显示数据的按钮和另一个带有 c# 代码的按钮,用于将相同的字段导出到 excel 中。网格显示数据源如下:

<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>

当我将属性 Visible="true" 添加到 PicLink 时,这是一个超链接,例如 http://beta.example.co.za/trials/Pictures/V_2992.jpg,该字段将显示在数据网格视图上,并显示 Excel 导出。如果我设置 Visible="false",数据网格和 Excel 导出都不会显示该字段。问题是我只想在 Excel 导出中显示该字段,而不是在数据网格上显示。

“导出到 Excel”按钮后面的 C# 代码如下所示:

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();

//导出到Excel

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
}

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:

    仅在 Excel 导出中:

    在 gvLogDetails(我想在 aspx 中)添加

    Visible="false"
    

    在第 16 列。 你可以删除

    grdExport.AutoGenerateColumns = false;
    

    你必须重新绑定数据。

    你也可以直接使用gvLogDetails。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-11
      • 2020-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多