要使用的类命名空间名称
using System.IO;
using System.Text;
using System.Data.OleDb;
using System.Drawing;
//////////////////////////////////////////////////////////////////////////////
前台<%@ Page Language="C#" AutoEventWireup="true" CodeFile="HistoryData.aspx.cs" Inherits="HistoryData" EnableEventValidation = "false" %>
中要加入EnableEventValidation = "false" ,导出时不会出错误。
//////////////////////////////////////////////////////////////////////////////
下面是我导出一个GridView,就是把数据放在dataset里面显示在gridView上,你的datatable应该也行。
//路灯历史记录导出Excel
protected void Export_Click(object sender, EventArgs e) //路灯信息导出Excel按钮
{
// 换成 export("application/ms-word", "路灯信息.doc"); 那么导出的就是Word格式的了.
Exportexcel("application/ms-excel", "路灯开关历史记录表.xls");
}
private void Exportexcel(string FileType, string FileName)
{
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
Response.ContentType = FileType;
this.EnableViewState = false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
SingleControl.AllowPaging = false;//将分页功能先关闭绑定,然后再开启绑定
BindSingleControl();
SingleControl.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
SingleControl.AllowPaging = true;
}
public override void VerifyRenderingInServerForm(Control control)
{
///此函数重要勿删
}
温馨提示:答案为网友推荐,仅供参考