将gridview更改为excel工作表,但链接按钮不应显示在excel工作表中,也不应在该行中显示. [英] changing gridview to excel sheet but the linkbuttons should not be shown in the excel sheet and that rows also.
本文介绍了将gridview更改为excel工作表,但链接按钮不应显示在excel工作表中,也不应在该行中显示.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在运行时将gridview数据转换为excel工作表.
我的gridview有一些不应该在excel工作表中显示的链接按钮.我已经做到了,但是具有链接按钮的行显示在工作表中,我们可以在将其转换为excel工作表时删除这些行.
i m converting the gridview data into the excel sheet at run time.
my gridview has some linkbutton that should not be shown in the excel sheet.i have done that but the rows that has that linkbuttons are showing in the sheet can we remove that rows when converting that into excel sheet. help me?
推荐答案
这是我在代码中隐藏列的操作,可能会对您有所帮助
This is what i did in my code to hide columns, might help you
private void ExportMyAppointmentasCSV(GridView grdGridView, int clinicianID)
{
DataSet ds = new DataSet();
clsAppointmentDb objAppTypeDb = new clsAppointmentDb();
ds = objAppTypeDb.GetAppointmentsbyClinician(clinicianID);
grdGridView.AllowPaging = false;
grdGridView.PageIndex = 0;
grdGridView.DataSource = ds.Tables[0];
grdGridView.DataBind();
if (grdGridView.Rows.Count > 0)
{
grdGridView.GridLines = GridLines.Both;
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=MyClinic.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.xls";
//For removing the Hyperlink from the Header of Grid View Before Writing to Excel
if (grdGridView.HeaderRow != null)
{
PrepareControlForExport(grdGridView.HeaderRow);
}
StringWriter stringWrite = new StringWriter();
HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
//This is Used to remove the columns like check box and Edit Delete coloumn
grdGridView.Columns[8].Visible = false;
grdGridView.Columns[0].Visible = false;
grdGridView.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.Flush();
Response.End();
}
导出按钮后面的代码
Code behind the export button
Protected Sub cmddown_Click_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmddown.Click
'#####ALL DATA####
dg.Columns(0).Visible = False
Dim form As HtmlForm = New HtmlForm
Dim attachment As String = "attachment; filename=Employee.xls"
Response.ClearContent()
Response.AddHeader("content-disposition", attachment)
Response.ContentType = "application/ms-excel"
Dim stw As System.IO.StringWriter = New System.IO.StringWriter
Dim htextw As HtmlTextWriter = New HtmlTextWriter(stw)
form.Controls.Add(dg)
Me.Controls.Add(form)
DisableControls(dg)
form.RenderControl(htextw)
Response.Write(stw.ToString)
Response.End()
'#####ALL DATA####
''#### ORI ####
'Response.Clear()
'Dim filename As String = "filename_" & Strings.Format(DateTime.Today, "MM-dd-yyyy") & ".xls"
'Response.Clear()
'Response.AddHeader("content-disposition", "attachment; filename=" & filename)
'Response.Charset = ""
'Response.ContentType = "application/vnd.xls"
'Me.EnableViewState = False
'Dim stringWrite As System.IO.StringWriter = New System.IO.StringWriter
'Dim htmlWrite As System.Web.UI.HtmlTextWriter = New HtmlTextWriter(stringWrite)
'Response.Buffer = False
''ClearControls(dg)
'dg.RenderControl(htmlWrite)
'Response.Write(stringWrite.ToString())
'Response.End()
''#### ORI ####
End Sub
禁用datagrid上所有服务器端控制的代码
Code for disable all server side control on the datagrid
Private Sub DisableControls(ByVal gv As Control)
Dim lb As LinkButton = New LinkButton
Dim l As Literal = New Literal
Dim name As String = String.Empty
Dim i As Integer = 0
Do While (i < gv.Controls.Count)
If (gv.Controls(i).GetType Is GetType(LinkButton)) Then
CType(gv.Controls(i), LinkButton).ForeColor = Color.Black
l.Text = CType(gv.Controls(i), LinkButton).Text
gv.Controls.Remove(gv.Controls(i))
gv.Controls.AddAt(i, l)
ElseIf (gv.Controls(i).GetType Is GetType(DropDownList)) Then
l.Text = CType(gv.Controls(i), DropDownList).SelectedItem.Text
gv.Controls.Remove(gv.Controls(i))
gv.Controls.AddAt(i, l)
End If
If gv.Controls(i).HasControls Then
DisableControls(gv.Controls(i))
End If
i = (i + 1)
Loop
End Sub
v1c3r
v1c3r
这篇关于将gridview更改为excel工作表,但链接按钮不应显示在excel工作表中,也不应在该行中显示.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文