.net 导出到excel

来源:百度知道 编辑:UC知道 时间:2024/06/28 03:11:53
private void ExportExcel(string filename)
{
//定义文档类型、字符编码
Response.Clear();
Response.Buffer = true;
Response.Charset = "GB2312";
//下面这行很重要, attachment 参数表示作为附件下载,您可以改成 online在线打开
//filename=FileFlow.xls 指定输出文件的名称,注意其扩展名和指定文件类型相符,可以为:.doc .xls .txt .htm
Response.AppendHeader("Content-Disposition", "attachment;filename="+filename+"");
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
//Response.ContentType指定文件类型 可以为application/ms-excel、application/ms-word、application/ms-txt、application/ms-html 或其他浏览器可直接支持文档

Response.ContentType ="application/ms-excel";
this.EnableViewState = false;
// 定义一个输入流
System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN", true);
System.IO

这样~
Response.Clear();
Response.AddHeader("content-disposition", "online;filename=test.xls");
Response.ContentType = "appilication/vnd.ms-excel";
Response.Charset = "";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
GridView dg = new GridView();
dg.DataSource = ds;
dg.DataBind();
dg.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();