1
我有一個顯示在asp.net柵格和與輸出相同場到Excel它後面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>
當我添加到PicLink,這是一個超級鏈接例如http://beta.example.co.za/trials/Pictures/V_2992.jpg屬性可見=「真」,該字段將顯示在數據網格視圖,並且還顯示Excel導出。如果我將Visible設置爲「false」,則數據網格和Excel導出都不顯示該字段。問題是我只想在Excel導出中顯示字段,但不是在數據網格上。
背後的C#代碼 '導出到Excel' 按鈕,如下所示:
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
}