如何从C#中的网格视图中删除数据导出中的前两列excel [英] How to delete first two columns from a data export to excel from grid view in C#
本文介绍了如何从C#中的网格视图中删除数据导出中的前两列excel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个.Net应用程序,它将网格视图数据导出到Excel工作表。我在网格视图中有两个按钮编辑和删除。在将数据导出到Excel时,这两个按钮也即将推出。我们可以删除这些按钮并仅保留excel中的值。请帮助。
我尝试过的方法:
I have a .Net application which exports the grid view data to an excel sheet. I have two buttons "Edit" and "Delete" inside the grid view. While exporting the data to excel these 2 buttons are also coming. Can we delete these buttons and keep only the values in excel.Please assist.
What I have tried:
private void ExportToExcel()
{
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
using (StringWriter sw = new StringWriter())
{
HtmlTextWriter hw = new HtmlTextWriter(sw);
//To Export all pages
grdChangeRequirement.AllowPaging = false;
this.ShowData();
grdChangeRequirement.HeaderRow.BackColor = Color.White;
foreach (TableCell cell in grdChangeRequirement.HeaderRow.Cells)
{
cell.BackColor = grdChangeRequirement.HeaderStyle.BackColor;
}
foreach (GridViewRow row in grdChangeRequirement.Rows)
{
row.BackColor = Color.White;
foreach (TableCell cell in row.Cells)
{
if (row.RowIndex % 2 == 0)
{
//cell.BackColor = grdChangeRequirement.AlternatingRowStyle.BackColor;
cell.BackColor = grdChangeRequirement.RowStyle.BackColor;
}
else
{
cell.BackColor = grdChangeRequirement.RowStyle.BackColor;
}
cell.CssClass = "textmode";
}
}
grdChangeRequirement.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();
}
}
推荐答案
检查这个 Gridview导出到Excel隐藏列 [ ^ ]
您可能最好使用for循环而不是foreach,这样您可以设置列的起始索引。代码可能需要调整,但这是一般的想法。
You might be better off using a for loop instead of foreach that way you can set the start index of your columns. The code might need a tweak, but that is the general idea.
for (int i = 2; i < row.Cells.Count; i++)
{
if (row.RowIndex % 2 == 0)
{
//cell.BackColor = grdChangeRequirement.AlternatingRowStyle.BackColor;
cell.BackColor = grdChangeRequirement.RowStyle.BackColor;
}
else
{
cell.BackColor = grdChangeRequirement.RowStyle.BackColor;
}
cell.CssClass = "textmode";
}
这篇关于如何从C#中的网格视图中删除数据导出中的前两列excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文