3个回答
展开全部
要使用的类命名空间名称
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)
{
///此函数重要勿删
}
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)
{
///此函数重要勿删
}
展开全部
只能先导出Excel在服务端然后提供Excel的下载。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
//导出
protected void btnOut_Click(object sender, EventArgs e)
{
// 当前对话
System.Web.HttpContext curContext = System.Web.HttpContext.Current;
// IO用于导出并返回excel文件
System.IO.StringWriter strWriter = null;
System.Web.UI.HtmlTextWriter htmlWriter = null;
curContext.Response.Clear();
curContext.Response.Buffer= true;
// 设置了类型为中文防止乱码的出现
curContext.Response.Charset="GB2312";
System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN",true);
// 设置输出流为简体中文
curContext.Response.ContentEncoding=System.Text.Encoding.GetEncoding("UTF-8");
// 定义输出文件和文件名
curContext.Response.AppendHeader("Content-Disposition","attachment;filename="+ "QueryResult" +".xls");
// 设置编码和附件格式
curContext.Response.ContentType = "application/vnd.ms-excel";
this.EnableViewState = false;
// 导出excel文件
strWriter = new System.IO.StringWriter(myCItrad);
htmlWriter = new System.Web.UI.HtmlTextWriter(strWriter);
// 返回客户端
Panel1.RenderControl(htmlWriter);
curContext.Response.Write(strWriter.ToString());
curContext.Response.End();
}
protected void btnOut_Click(object sender, EventArgs e)
{
// 当前对话
System.Web.HttpContext curContext = System.Web.HttpContext.Current;
// IO用于导出并返回excel文件
System.IO.StringWriter strWriter = null;
System.Web.UI.HtmlTextWriter htmlWriter = null;
curContext.Response.Clear();
curContext.Response.Buffer= true;
// 设置了类型为中文防止乱码的出现
curContext.Response.Charset="GB2312";
System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN",true);
// 设置输出流为简体中文
curContext.Response.ContentEncoding=System.Text.Encoding.GetEncoding("UTF-8");
// 定义输出文件和文件名
curContext.Response.AppendHeader("Content-Disposition","attachment;filename="+ "QueryResult" +".xls");
// 设置编码和附件格式
curContext.Response.ContentType = "application/vnd.ms-excel";
this.EnableViewState = false;
// 导出excel文件
strWriter = new System.IO.StringWriter(myCItrad);
htmlWriter = new System.Web.UI.HtmlTextWriter(strWriter);
// 返回客户端
Panel1.RenderControl(htmlWriter);
curContext.Response.Write(strWriter.ToString());
curContext.Response.End();
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询