惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

I
InfoQ
博客园_首页
美团技术团队
M
MIT News - Artificial intelligence
人人都是产品经理
人人都是产品经理
Blog — PlanetScale
Blog — PlanetScale
H
Help Net Security
J
Java Code Geeks
T
Tailwind CSS Blog
Jina AI
Jina AI
量子位
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
G
Google Developers Blog
爱范儿
爱范儿
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
宝玉的分享
宝玉的分享
小众软件
小众软件
MongoDB | Blog
MongoDB | Blog
博客园 - 三生石上(FineUI控件)
L
LangChain Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
V
Visual Studio Blog
博客园 - Franky
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知

博客园 - 庞滨

查询优化 什么是iBATIS(一) 什么是iBATIS(二) 李彦宏在北大2008本科生毕业典礼上的发言 成长 何为sq注入 关闭网页框架父窗口,并跳转到一个新的窗口。 如何清除Session/设置Session超时, 如何显示当前的访问人数和总访问量. C#中数据库的连接 页面与页面之间如何传递值?如何获得 URL传过来的值. ASP.NET的错误处理机制 CSS样式 GridView的用法 使GidView变色 GridView中的双向排序 C#如何生成一个XML文件,并保存在硬盘的指定目录下 精美的HTML万年历 点击输入日期的TxtBox框后弹出js日期框供选择 C#正则表达式
导出excel
庞滨 · 2008-11-14 · via 博客园 - 庞滨

这种方式的原理是,在某页面(任何页面比如a.htm),把要导出的内容或结果集,通过表单提交到一个b.aspx页面。
aspx页面中,生成一个具有唯一文件名的excel文件,然后自动输出到前台。由前台来保存。
1.
a.htm页面,只需要把表单提交到b.aspx中即可。至于要提交什么内容,放入表单中的hidden控件即可。
你甚至可以动态改变hidden控件的value的值(这个很有用)。
关键字:
<form method="post" action="b.aspx" target="_blank">
<input type="hidden" name="TextSql" value="要提交的内容" ID="TextSql"></input>
</form>

2.
在aspx页面后台关键代码:


using System.IO;
using System.Text;

 // 接收表单的值
  private void Page_Load(object sender, System.EventArgs e)
  {
  
   string destString = Request.Form["TextSql"].ToString();
   ExportToFile(destString);

  }
 

//存为excel文件
 private void ExportToFile(string esql)
  {
   //new 一个 guid
   string FileName = Guid.NewGuid().ToString() + ".xls"; //GUID生成唯一文件名
   string FileType="application/ms-excel";
   try
   {
    System.Web.HttpResponse httpResponse = Page.Response;
    httpResponse.AppendHeader("Content-Disposition","attachment;filename="+HttpUtility.UrlEncode(FileName,System.Text.Encoding.UTF8));
    httpResponse.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");
    httpResponse.ContentType = FileType;
    StringBuilder ckpw = new StringBuilder(esql);
    System.IO.StringWriter  tw = new System.IO.StringWriter(ckpw) ;
    string filePath = Server.MapPath("..")+"\\"+FileName;
    System.IO.StreamWriter sw = System.IO.File.CreateText(filePath);
    sw.Write(tw.ToString());
    sw.Close();

    DownFile(httpResponse,FileName,filePath);
    httpResponse.End();
   }
   catch (Exception ee)
   {
    throw ee;
   }    
  }


//输出到前台  
private  bool DownFile(System.Web.HttpResponse Response,string fileName,string fullPath)
  {
   try
   {
    Response.ContentType = "application/octet-stream";

    Response.AppendHeader("Content-Disposition","attachment;filename=" +
     HttpUtility.UrlEncode(fileName,System.Text.Encoding.UTF8) + ";charset=GB2312");
    System.IO.FileStream fs= System.IO.File.OpenRead(fullPath);
    long fLen=fs.Length;
    int size=102400;//每100K同时下载数据
    byte[] readData = new byte[size];//指定缓冲区的大小
    if(size>fLen)size=Convert.ToInt32(fLen);
    long fPos=0;
    bool isEnd=false;
    while (!isEnd)
    {
     if((fPos+size)>fLen)
     {
      size=Convert.ToInt32(fLen-fPos);
      readData = new byte[size];
      isEnd=true;
     }
     fs.Read(readData, 0, size);//读入一个压缩块
     Response.BinaryWrite(readData);
     fPos+=size;
    }
    fs.Close();
    System.IO.File.Delete(fullPath);
    return true;
   }
   catch
   {
    return false;
   }
  }

Feedback

#1楼    回复  引用    

2007-12-14 09:50 by kitwen [未注册用户]

<form method="post" action="b.aspx" target="_blank">
<input type="hidden" name="TextSql" value="要提交的内容" ID="TextSql"></input>
</form>

1.要提交的内容要用什么形式?html?二进制流?
2.为什么不可以直接在b.aspx写要输出的内容呢?

#2楼 [楼主]   回复  引用  查看    

2008-04-02 11:40 by 暴走的猪      

@kitwen
1.要提交的内容你可以动态写入 hidden控件内。可以是形式合法的html table或者xml
2.当然可以直接用b.aspx 。。我这个是在htm页面要导出到excecl,所以得把它提交倒b.aspx