导出Excel(防止导出整个页面)
2012-03-23 15:31
351 查看
前台页面放一个GridView什么的就不说了,要注意的是在
<%@ Page Language="C#" AutoEventWireup="true" Codebehind="ReferPriceIndex.aspx.cs"EnableEventValidation="false"
Inherits="ZTE.Fol.Fund.UI.Web.InsideTrade.ReferPrice.ReferPriceIndex" %>
标签里面加红色字体的那个属性
同时后台加上
/// <summary>
/// 内容摘要:重写空的VerifyRenderingInServerForm方法,避免在导出Excel文件的时候出现
/// “……必须放在具有 runat=server 的窗体标记内”的异常
/// 另外说明:
/// 在asp.net2.0中,控件的校验严格了,
/// RenderControl代码只有走正常流程在render方法中它自己调用才能成功,
/// 在自己写的事件方法中调用就会出现这个错误。
/// </summary>
/// <param name="control">提交控件</param>
public override void VerifyRenderingInServerForm(Control control)
{
}// end VerifyRenderingInServerForm
这两个地方都加上,可以防止导出excel的时候导出整个页面
下面将将具体方法贴出来
/// <summary>
/// 导出数据到EXCEL文件
/// </summary>
/// <param name="page">页面</param>
/// <param name="gvExcel">导出的GrivView</param>
private void ExportToExcel(System.Web.UI.Page page, GridView dgExcel)
{
try
{
foreach (GridViewRow row in dgExcel.Rows)
{
foreach (TableCell cell in row.Cells)
{
cell.Style.Add("vnd.ms-excel.numberformat", "@");//给表格内容设置样式
} // end foreach (TableCell cell in row.Cells)
} // end foreach (GridViewRow row in dgExcel.Rows)
string fileName = "OutData.xls";
page.Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName);
Response.Clear();
Response.Buffer = true;
Response.Charset = "utf-8";
Response.Write("<meta http-equiv=Content-Type content=text/html;charset=utf-8>");
Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
Response.ContentType = "application/ms-excel";
dgExcel.Page.EnableViewState = false;
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new HtmlTextWriter(tw);
dgExcel.RenderControl(hw);
// 输出DataGrid内容
Response.Write(tw.ToString());
Response.End();
}
catch (Exception ex)
{
ZTE.Fol.Framework.Common.Util.WebUtil.MessageBox(this.Page, ex.Message);
}
}
<%@ Page Language="C#" AutoEventWireup="true" Codebehind="ReferPriceIndex.aspx.cs"EnableEventValidation="false"
Inherits="ZTE.Fol.Fund.UI.Web.InsideTrade.ReferPrice.ReferPriceIndex" %>
标签里面加红色字体的那个属性
同时后台加上
/// <summary>
/// 内容摘要:重写空的VerifyRenderingInServerForm方法,避免在导出Excel文件的时候出现
/// “……必须放在具有 runat=server 的窗体标记内”的异常
/// 另外说明:
/// 在asp.net2.0中,控件的校验严格了,
/// RenderControl代码只有走正常流程在render方法中它自己调用才能成功,
/// 在自己写的事件方法中调用就会出现这个错误。
/// </summary>
/// <param name="control">提交控件</param>
public override void VerifyRenderingInServerForm(Control control)
{
}// end VerifyRenderingInServerForm
这两个地方都加上,可以防止导出excel的时候导出整个页面
下面将将具体方法贴出来
/// <summary>
/// 导出数据到EXCEL文件
/// </summary>
/// <param name="page">页面</param>
/// <param name="gvExcel">导出的GrivView</param>
private void ExportToExcel(System.Web.UI.Page page, GridView dgExcel)
{
try
{
foreach (GridViewRow row in dgExcel.Rows)
{
foreach (TableCell cell in row.Cells)
{
cell.Style.Add("vnd.ms-excel.numberformat", "@");//给表格内容设置样式
} // end foreach (TableCell cell in row.Cells)
} // end foreach (GridViewRow row in dgExcel.Rows)
string fileName = "OutData.xls";
page.Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName);
Response.Clear();
Response.Buffer = true;
Response.Charset = "utf-8";
Response.Write("<meta http-equiv=Content-Type content=text/html;charset=utf-8>");
Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
Response.ContentType = "application/ms-excel";
dgExcel.Page.EnableViewState = false;
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new HtmlTextWriter(tw);
dgExcel.RenderControl(hw);
// 输出DataGrid内容
Response.Write(tw.ToString());
Response.End();
}
catch (Exception ex)
{
ZTE.Fol.Framework.Common.Util.WebUtil.MessageBox(this.Page, ex.Message);
}
}
相关文章推荐
- 导出Excel(防止导出整个页面)
- 从Asp.net导出excel时防止连页面一起导出
- Asp.net GridView 导出Excel (不会整个页面导出且不会乱码)
- 页面EXCEL导出
- js页面上的excel导出
- 分享一个导出Excel时页面不跳转的小技巧
- asp.net 生成 excel导出保存时, 解决迅雷下载aspx页面问题
- [转载]jsp页面显示数据导出到excel表中
- asp.net页面指定控件导出为Word 或excel 文档
- jsp 页面导出excel时字符串数字变成科学计数法的解决方法
- JAVA 关于web页面下载导出Excel通用方法
- html页面表格导出到excel总结
- Vue页面table导出Excel
- 利用获取页面html,导出excel
- JavaScript将Web页面内容导出到Word及Excel的方法
- php导出数据到excel,防止身份证等数字字符格式变成科学计数的方法
- 前端:页面表格导出Excel
- 实现页面中的table导出成Excel格式
- WEB页面导出为EXCEL文档的方法</
- 页面table导出Excel——table2excel