CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
山寨机中的战斗机! 程序优化工程师到底对IT界有没有贡献
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  .NET技术 >  C#

有关ASP.NET 和 Excel 返回浏览器

楼主nerk(尘世中一个迷途小书僮)2002-11-20 15:19:56 在 .NET技术 / C# 提问

我要在服务器端用代码新建Excel文档,然后直接Response回客户端  
  以前看到过一个帖子有代码,现在找不到了。。。  
   
  有谁知道如何做?  
  我已经建好了Excel对象,Sheet里面也填充好了内容,就是不知道如何返回给客户端?(不在服务器保存)  
   
  记得有Response.ClearHeaders什么的。。。 问题点数:100、回复次数:2Top

1 楼TheAres(班门斧)回复于 2002-11-23 00:27:55 得分 100

Response.AddHeader("Content-Disposition",   strHeadValue);  
  Response.ContentType="Application/vnd.ms-excel";  
  Response.WriteFile(strFilePath);  
   
  参考:  
  http://support.microsoft.com/default.aspx?scid=KB;EN-US;q306654&ID=KB;EN-US;q306654&lex  
  Top

2 楼nerk(尘世中一个迷途小书僮)回复于 2002-11-25 15:30:49 得分 0

多謝,可能我說的不是很清楚.  
  我找到了解決方案:  
  17. '   Create   a   connection   and   open   it.  
  18. Dim   objConn   As   New   System.Data.SqlClient.SqlConnection("User   ID=sa;Initial   Catalog=Northwind;Data   Source=SQLServer;")  
  19. objConn.Open()  
  20.  
  21. Dim   strSQL   As   String  
  22. Dim   objDataset   As   New   DataSet()  
  23. Dim   objAdapter   As   New   System.Data.SqlClient.SqlDataAdapter()  
  24.  
  25. '   Get   all   the   customers   from   the   USA.  
  26. strSQL   =   "Select   *   from   customers   where   country='USA'"  
  27. objAdapter.SelectCommand   =   New   System.Data.SqlClient.SqlCommand(strSQL,   objConn)  
  28. '   Fill   the   dataset.  
  29. objAdapter.Fill(objDataset)  
  30. '   Create   a   new   view.  
  31. Dim   oView   As   New   DataView(objDataset.Tables(0))  
  32. '   Set   up   the   data   grid   and   bind   the   data.  
  33. DataGrid1.DataSource   =   oView  
  34. DataGrid1.DataBind()  
  35.  
  36. '   Verify   if   the   page   is   to   be   displayed   in   Excel.  
  37. If   Request.QueryString("bExcel")   =   "1"   Then  
  38.         '   Set   the   content   type   to   Excel.  
  39.         Response.ContentType   =   "application/vnd.ms-excel"  
  40.         '   Remove   the   charset   from   the   Content-Type   header.  
  41.         Response.Charset   =   ""  
  42.         '   Turn   off   the   view   state.  
  43.         Me.EnableViewState   =   False  
  44.  
  45.         Dim   tw   As   New   System.IO.StringWriter()  
  46.         Dim   hw   As   New   System.Web.UI.HtmlTextWriter(tw)  
  47.  
  48.         '   Get   the   HTML   for   the   control.  
  49.         DataGrid1.RenderControl(hw)  
  50.         '   Write   the   HTML   back   to   the   browser.  
  51.         Response.Write(tw.ToString())  
  52.         '   End   the   response.  
  53.         Response.End()  
  Top

相关问题

  • 求救,浏览器不能正确显示asp。net页面
  • asp问题,浏览器不执行asp代码
  • jexcelapi怎么样在浏览器中显示excel文件啊?
  • 如何让浏览器直接下载一个文件,而不是打开它,比如word文档.要ASP的解决方案,不要.net
  • 浏览器还是asp程序的问题
  • 奇怪!!!IE浏览器还是asp的问题?
  • ASP中如何获得用户浏览器的分辨率??????
  • asp中怎么判断客户端浏览器的类型。(急)
  • 如何用ASP在浏览器中显示地图
  • 如何用ASP在浏览器中显示地图

关键词

  • excel
  • objadapter
  • objdataset
  • objconn
  • sqlclient
  • strsql
  • dim
  • response
  • data

得分解答快速导航

  • 帖主:nerk
  • TheAres

相关链接

  • CSDN .NET频道
  • .NET类图书
  • C#类图书
  • .NET类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo