在ASP.NET C中仅将gridview数据导出为excel格式# [英] Export only gridview data to excel format in ASP.NET C#
问题描述
在我的页面中,我有两个文本框控件,我从Calendar Extender& amp;中选择日期。在导出到Excel的按钮我将导出Gridview数据到Excel工作表。当我选择excel表时,它会显示文本框和文本框。按钮也从我导出excel表。
我已在导出按钮中写入导出代码。喜欢: -
protected void Export_to_Excel_Click( object sender,EventArgs e)
{
try
{
Response.ClearContent();
Response.Buffer = true ;
Response.AddHeader( content-disposition, string .Format( attachment; filename = {0}, Customers.xls));
Response.ContentType = application / ms-excel;
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
Grd_MidData.AllowPaging = false ;
bindgriddata();
// 将标题行更改回白色
Grd_MidData.HeaderRow。 Style.Add( background-color, #FFFFFF);
// 将stlye应用于gridview标题单元格
for ( int i = 0 ; i < Grd_MidData.HeaderRow.Cells.Count; i ++)
{
Grd_MidData.HeaderRow.Cells [i] .Style.Add( background-color, #df5015\" 跨度>);
}
Grd_MidData.RenderControl(htw);
Response.Write(sw.ToString());
Response.Flush();
}
catch (例外ee)
{
}
}
当我选择excel表时,它会显示gridview数据以及两个文本框&我正在进行过滤的按钮。
所以任何人都建议我,如何仅显示gridview数据但不显示Textbox& excel表上的按钮。
我尝试过:
这里显示gridview数据以及From date:To Date:textbox&按钮也。那么如何从Excel工作表中删除这些字段。
试试这个
受保护 void Export_to_Excel_Click( object sender,EventArgs e)
{
try
{
GridView gv = new GridView();
Grd_MidData.AllowPaging = false ;
BindCustomersDDL();
gv = Grd_MidData; // 克隆现有网格,以免打扰现有网格
Response.ClearContent();
Response.Buffer = true ;
Response.AddHeader( content-disposition, string .Format( attachment; filename = {0}, Customers.xls));
Response.ContentType = application / ms-excel;
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
for ( int i = 0 ; i < Grd_MidData.HeaderRow.Cells.Count; i ++)
gv.HeaderRow.Cells [i] .Style.Add ( background-color, #df5015);
// 隐藏excel输出中不需要的列
int [] columnsToBeHidden = { 2 , 8 }; // 零基础索引,这将隐藏第3个广告第9列
for ( int i = 0 ; i < columnsToBeHidden.Length; i ++)
gv.Columns [i] .Visible = false ;
gv.RenderControl(htw);
Response.Write(sw.ToString());
Response.Flush();
}
catch (例外ee)
{
}
}
传递要隐藏的列的索引
int [] columnsToBeHidden = { 5 , 6 };
In my page I have two textbox controls, where I am gating the date from the Calendar Extender & in the export to excel Button I am going to export the Gridview data to excel sheet. When I am gating the excel sheet its show the the textbox & button also from which i am export the excel Sheet.
I have written the export code in the Export Button. Like:-
protected void Export_to_Excel_Click(object sender, EventArgs e)
{
try
{
Response.ClearContent();
Response.Buffer = true;
Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "Customers.xls"));
Response.ContentType = "application/ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
Grd_MidData.AllowPaging = false;
bindgriddata();
//Change the Header Row back to white color
Grd_MidData.HeaderRow.Style.Add("background-color", "#FFFFFF");
//Applying stlye to gridview header cells
for (int i = 0; i < Grd_MidData.HeaderRow.Cells.Count; i++)
{
Grd_MidData.HeaderRow.Cells[i].Style.Add("background-color", "#df5015");
}
Grd_MidData.RenderControl(htw);
Response.Write(sw.ToString());
Response.Flush();
}
catch (Exception ee)
{
}
}
When I am gating the excel sheet it shows the gridview data along with the two text box & the button from where I am doing the filtering.
So any one suggest me, How to show only the gridview data but not show the Textbox & button on the excel sheet.
What I have tried:
Here the gridview data is showing along with the From date: To Date: textbox & the button also. So how can i remove these field from the Excel sheet.
try this
protected void Export_to_Excel_Click(object sender, EventArgs e) { try { GridView gv = new GridView(); Grd_MidData.AllowPaging = false; BindCustomersDDL(); gv = Grd_MidData; // cloning the existing grid, so that it will not disturb the existing grid Response.ClearContent(); Response.Buffer = true; Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "Customers.xls")); Response.ContentType = "application/ms-excel"; StringWriter sw = new StringWriter(); HtmlTextWriter htw = new HtmlTextWriter(sw); for (int i = 0; i < Grd_MidData.HeaderRow.Cells.Count; i++) gv.HeaderRow.Cells[i].Style.Add("background-color", "#df5015"); // Hiding the unwanted columns in the excel output int[] columnsToBeHidden = { 2,8 }; // zero based index, this will hide 3rd ad 9th column for (int i = 0; i < columnsToBeHidden.Length; i++) gv.Columns[i].Visible = false; gv.RenderControl(htw); Response.Write(sw.ToString()); Response.Flush(); } catch (Exception ee) { } }
Pass the index of the column which you want to hide
int[] columnsToBeHidden = { 5,6};
这篇关于在ASP.NET C中仅将gridview数据导出为excel格式#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!