C# 将gridview的选定列发送到excel文件
我尝试过将整个gridview数据保存到excel文件的代码,它工作正常,但现在我只想将gridview的特定列保存到excel文件。我使用了mysql数据库和asp.net与C。请任何人帮助我C# 将gridview的选定列发送到excel文件,c#,excel,gridview,C#,Excel,Gridview,我尝试过将整个gridview数据保存到excel文件的代码,它工作正常,但现在我只想将gridview的特定列保存到excel文件。我使用了mysql数据库和asp.net与C。请任何人帮助我 **default.aspx** <asp:GridView ID="GridView1" runat="server"> </asp:GridView> <asp:Button ID="btnSave" runat="server" Text="Sav
**default.aspx**
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
<asp:Button ID="btnSave" runat="server" Text="Save to Excel" OnClick="btnSave_Click" />
**default.cs**
protected void btnSave_Click(object sender, EventArgs e)
{
ExportGridToExcel(GridView1, "StudentMarks.xls");
}
public void ExportGridToExcel(GridView grdGridView, string fileName)
{
Response.Clear();
Response.AddHeader("content-disposition", string.Format("attachment;filename={0}.xls", fileName));
Response.Charset = "";
Response.ContentType = "application/vnd.xls";
StringWriter stringWrite = new StringWriter();
HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
grdGridView.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();
}
public override void VerifyRenderingInServerForm(Control control)
{
return;
}
可以使用asp:boundfield选择要在gridview中显示的列。然后将该gridview导出到excel
好的。在您的情况下,您可以使用相同的数据绑定一个新的gridview,但不需要编辑/删除列。可以将此新gridview的可见性设置为false 要生成excel,请使用此方法
protected void btnExportExcel_Click(object sender, EventArgs e)
{
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition","attachment;filename=GridViewExport.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
GridView.AllowPaging = false;
// Re-Bind data to GridView
using (MSEntities1 CompObj = new MSEntities1())
{
// code for bind grid view
GridView.DataBind();
}
// Change the Header Row back to white color
GridViewC.Style.Add(" font-size", "10px");
GridView.HeaderRow.Style.Add("background-color", "#FFFFFF");
//Apply style to Individual Cells
GridView.HeaderRow.Cells[0].Style.Add("background-color", "green");
GridView.HeaderRow.Cells[1].Style.Add("background-color", "green");
GridView.HeaderRow.Cells[2].Style.Add("background-color", "green");
GridView.HeaderRow.Cells[3].Style.Add("background-color", "green");
GridView.HeaderRow.Cells[4].Style.Add("background-color", "green");
GridView.HeaderRow.Cells[5].Style.Add("background-color", "green");
GridView.HeaderRow.Cells[6].Style.Add("background-color", "green");
GridView.HeaderRow.Cells[7].Style.Add("background-color", "green");
int k = GridView.Rows.Count;
for (int i = 1; i < GridView.Rows.Count; i++)
{
GridViewRow row = GridView.Rows[i];
//Change Color back to white
row.BackColor = System.Drawing.Color.White;
//Apply text style to each Row
row.Attributes.Add("class", "textmode");
//Apply style to Individual Cells of Alternating Row
if (i % 2 != 0)
{
row.Cells[0].Style.Add("background-color", "#C2D69B");
row.Cells[1].Style.Add("background-color", "#C2D69B");
row.Cells[2].Style.Add("background-color", "#C2D69B");
row.Cells[3].Style.Add("background-color", "#C2D69B");
row.Cells[4].Style.Add("background-color", "#C2D69B");
row.Cells[5].Style.Add("background-color", "#C2D69B");
row.Cells[6].Style.Add("background-color", "#C2D69B");
row.Cells[7].Style.Add("background-color", "#C2D69B");
}
}
GridView.RenderControl(hw);
// style to format numbers to string.
string style = @"<style> .textmode { mso-number-format:\@; } </style>";
Response.Write(style);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
}
显示您的代码并告诉人们您到目前为止尝试了什么。此链接解决方案可以解决您的问题吗?您可以使用上面评论链接中提到的方法生成excel。但是,在此链接中,无法选择gridview的特定列,然后仅将这些列保存到excel文件。您可以告诉我如何生成excel的完整代码吗只将选定的列保存到excel文件实际上我有一个gridview,其中第一列使用了两个图像,一个用于编辑,另一个用于删除操作。因此,现在当我应用上述代码时,我也将获得编辑/删除列。但我只想将数据保存在excel文件中,而不是该列。请告诉我如何保存数据为了实现这一点,我知道这是一个解决方案,或者我们也可以使用dataset对象,而不是gridview,但如果我只想将gridview的特定列保存到excel文件中,该怎么办?检查此链接我已尝试过此链接,但对我来说效果不好…如果您可以,请从中获得帮助,但请为我提供MySQL的此代码。您可以检查链接吗张贴。无法打开它。但在此方法中,我们将检查要保存到excel文件的gridview的哪些列?要存储在excel中的哪些列,根据上面在我的gridview中看到的标准,我有第一列要编辑/删除,现在我不想将此列保存在excel文件中确定,然后您可以根据行id在函数中重新绑定数据,然后生成excel,就像我在此处重新绑定所有数据一样您可以在excel中放置查询并获取所需的列-//使用MSEntities1 CompObj=new MSEntities1将数据重新绑定到GridView{//bind grid view GridView.DataBind;}您能详细解释一下并给出相同的示例代码吗