文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>将ASP.NET Web页面的数据导出为Excel

将ASP.NET Web页面的数据导出为Excel

时间:2010-09-19  来源:暗※空

Aspx页面:

 

代码
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="MgrSummaryPrint.aspx.cs"  EnableEventValidation = "false" 
Inherits
="WebEdu.Main.MgrSummaryPrint" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>导出数据</title>
</head>
<body>
<form id="form1" runat="server">
<%--导出tbImportData中的内容--%>
<table runat="server" id="tbImportData" width="900" border="0" cellspacing="0" cellpadding="0" align="center">
<tr>
<td>
&nbsp;
</td>
</tr>
</table>
<img height="18" onclick="preview();" src="images/dayin.gif" style="cursor: hand; border: none;"
width
="48" />
</body>
</html>

 

 

cs Code页面:

代码
protected void ImgBtnImportDataExcel_Click(object sender, ImageClickEventArgs e)
{
Response.Clear();
Response.AddHeader(
"content-disposition", "attachment;filename=FileName.xls");
Response.Charset
= "UTF-8";
Response.ContentType
= "application/vnd.xls";
System.IO.StringWriter stringWrite
= new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite
= new HtmlTextWriter(stringWrite);
//gvSumPrint.AllowPaging = false;/*若Gridview启用了分页,先关闭分页再重新绑定数据,再导出数据*/

string sql = "select * from table1 join t1_MgrInfo ";

DataTable drpDistrict
= GetTable(sql1);
DataTable dt
= GetTable(sql);
this.gvSumPrint.DataSource = dt;
this.gvSumPrint.DataBind();

tbImportData.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();
}


public override void VerifyRenderingInServerForm(Control ct)
{

}

//将纯数值设置为文本形式存储到Excel中
protected void gvSumPrint_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[
2].Attributes.Add("style", "vnd.ms-excel.numberformat:@");
}
}

 

相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载