我在ASP.Net应用程序中使用AutoGenerateDeleteButton& AutoGenerateEditButton设置为True。我想在将gridview导出到excel时不要在Excel工作表中显示这些按钮。我的导出代码如下:
Private Sub ExportGridView()
Dim attachment As String = "attachment; filename=FileName.xls"
Response.ClearContent()
Response.AddHeader("content-disposition", attachment)
Response.ContentType = "application/ms-excel"
Dim sw As New IO.StringWriter
Dim frm As HtmlForm = New HtmlForm()
Page.EnableViewState = False
Dim htw As HtmlTextWriter = New HtmlTextWriter(sw)
frm.Attributes("runat") = "server"
Controls.Add(frm)
gvResults.AllowPaging = False
gvResults.AllowSorting = False
BindGrid()
gvResults.DataBind()
gvResults.Columns(14).Visible = False
gvResults.Columns(15).Visible = False
gvResults.Columns(16).Visible = False
frm.Controls.Add(gvResults)
frm.RenderControl(htw)
Response.Write(sw.ToString())
Response.End()
End Sub
答案 0 :(得分:0)
搜索后我想出了这个
protected void Button1_Click(object sender, EventArgs e)
{
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=User.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
using (StringWriter sw = new StringWriter())
{
HtmlTextWriter hw = new HtmlTextWriter(sw);
GridView1.AllowPaging = false;
GridView1.AllowSorting = false;
GridView1.Columns[14].Visible = false;
GridView1.Columns[15].Visible = false;
GridView1.Columns[16].Visible = false;
BindGird();
GridView1.HeaderRow.BackColor = Color.White;
foreach (TableCell cell in GridView1.HeaderRow.Cells)
{
cell.BackColor = GridView1.HeaderStyle.BackColor;
}
foreach (GridViewRow row in GridView1.Rows)
{
row.BackColor = Color.White;
foreach (TableCell cell in row.Cells)
{
if (row.RowIndex % 2 == 0)
{
cell.BackColor = GridView1.AlternatingRowStyle.BackColor;
}
else
{
cell.BackColor = GridView1.RowStyle.BackColor;
}
cell.CssClass = "textmode";
}
}
GridView1.RenderControl(hw);
//style to format numbers to string
string style = @"<style> .textmode { } </style>";
Response.Write(style);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
GridView1.Dispose();
}
}
public override void VerifyRenderingInServerForm(Control control)
{
/* Verifies that the control is rendered */
}
并在aspx页面上进行更改,如
<%@ Page Title="" Language="C#" EnableEventValidation="false"%>
在vb中,我猜它看起来像这样
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
Response.Clear
Response.Buffer = true
Response.AddHeader("content-disposition", "attachment;filename=User.xls")
Response.Charset = ""
Response.ContentType = "application/vnd.ms-excel"
Dim sw As StringWriter = New StringWriter
Dim hw As HtmlTextWriter = New HtmlTextWriter(sw)
GridView1.AllowPaging = false
GridView1.AllowSorting = false
GridView1.Columns(14).Visible = false
GridView1.Columns(15).Visible = false
GridView1.Columns(16).Visible = false
BindGird
GridView1.HeaderRow.BackColor = Color.White
For Each cell As TableCell In GridView1.HeaderRow.Cells
cell.BackColor = GridView1.HeaderStyle.BackColor
Next
For Each row As GridViewRow In GridView1.Rows
row.BackColor = Color.White
For Each cell As TableCell In row.Cells
If ((row.RowIndex Mod 2) _
= 0) Then
cell.BackColor = GridView1.AlternatingRowStyle.BackColor
Else
cell.BackColor = GridView1.RowStyle.BackColor
End If
cell.CssClass = "textmode"
Next
Next
GridView1.RenderControl(hw)
'style to format numbers to string
Dim style As String = "<style> .textmode { } </style>"
Response.Write(style)
Response.Output.Write(sw.ToString)
Response.Flush
Response.End
GridView1.Dispose
End Sub
Public Overrides Sub VerifyRenderingInServerForm(ByVal control As Control)
End Sub
.aspx页面就像这样
<%@ Page Title="" Language="C#" EnableEventValidation="false"%>
答案 1 :(得分:0)
解决方案是在导出时将它们设置为false:
gvResults.AllowPaging = False
gvResults.AllowSorting = False
gvResults.AutoGenerateEditButton = False
gvResults.AutoGenerateDeleteButton = False
BindGrid()