这里记录一下ASP.NET 中添加导出Excel功能的方法。
//导出
protected void btnOutExcel_Click(object sender, EventArgs e)
{
if (rptList.Items.Count > 0)
{
//调用导出方法
ExportGridViewForUTF8("艺术品信息汇总");
}
else
{
PublicClass.ShowAlter(this.Page, "waring", "没有数据可导出!");
}
}
///
/// 导出方法
///
/// 保存的文件名称
private void ExportGridViewForUTF8(string filename)
{
Response.Clear();
Response.Buffer = true;
Response.Charset = "utf-8";
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8) + ".xls");
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312"); //设置输出流为简体中文
Response.ContentType = "application/ms-excel"; //设置输出文件类型为excel文件。
this.EnableViewState = false;
System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN", true);
System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad);
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
rptList.RenderControl(oHtmlTextWriter);
Response.Write("
Response.Write("
{0} | ", filename));||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
序号 | ");艺术品编号 | ");艺术品名称 | ");作者 | ");创作日期 | ");展览名称 | ");展出日期 | ");撤展日期 | ");所属展厅 | ");材质 | ");展品尺寸 | ");有源标签号 | ");无源标签号 | ");状态 | ");备注 | ");
Response.Write("
Response.End();
}