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>
</td>
</tr>
</table>
<img height="18" onclick="preview();" src="images/dayin.gif?http://www.xvna.com" 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:@");
}
}