CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
可用分押宝游戏火热进行中... 专题改版:Java Web 专题
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  Web 开发 >  ASP

急求,没人会吗?如何把页面中table内的数据保存为EXCEL文件

楼主iamsatyr(瑟瑟)2004-08-02 14:48:52 在 Web 开发 / ASP 提问

如题 问题点数:0、回复次数:6Top

1 楼dreammoci(小鱼儿)回复于 2004-08-02 15:07:34 得分 0

如果你table里的内容是从数据库里拿出来的,就这可这样写  
   
  fp   =   Server.MapPath(fileName)'你要存的EXCEL的文件名  
  Set   FSO   =   Server.CreateObject("Scripting.FileSystemObject")  
  Set   xlsFile   =   fso.CreateTextFile(fp,   true)  
   
  strOutput   =   "filed,filed1,filed2,filed3"  
  xlsFile.WriteLine(strOutput)  
  strOutput   =   "   "   &   rsAG("F1173")   &   ","   &   rsAG("F364")   &   ","   &   rsAG("F37")   &   ","   &   rsAG("F11")  
  xlsFile.WriteLine(strOutput)  
   
  如果不是,就把strOutput的值一条条写出来就行了。Top

2 楼colee(冷面 ◎ 虚者神头)回复于 2005-03-17 10:53:41 得分 0

http://community.csdn.net/Expert/topic/3569/3569209.xml?temp=.526333Top

3 楼lovebanyi(风云)回复于 2005-03-17 13:55:27 得分 0

<%    
   
  Set   xlApp   =   server.CreateObject("Excel.Application")    
   
  strsource   =   "test.xls"    
   
  Set   xlbook   =   xlApp.Workbooks.Open(strsource)    
  Set   xlsheet   =   xlbook.Worksheets(1)    
   
  i=1    
  response.write   "<table   cellpadding=0   cellspacing=0   border=1   width=500>"    
  while   xlsheet.cells(i,1)<>""    
   
  response.write   "<tr>"    
  response.write   "   <td   height=20   align=center   width=100>"   &   xlsheet.Cells(i,   1)   &   "</td>"    
  response.write   "   <td   height=20   align=center   width=200>"   &   xlsheet.Cells(i,   2)   &   "</td>"    
  response.write   "   <td   height=20   align=center   width=200>"   &   xlsheet.Cells(i,   3)   &   "</td>"    
  response.write   "</tr>"    
  i=i+1    
   
  wend    
  response.write   "</table>"    
  set   xlsheet=nothing    
  set   xlbook=nothing    
  xlApp.quit   %>Top

4 楼syd41()回复于 2005-03-17 14:08:39 得分 0

up!~~~~Top

5 楼ljqhbt(半个杭州人)(不爱QQ)回复于 2005-03-17 16:18:25 得分 0

<input   type="hidden"   name="out_word"   onclick="vbscript:buildDoc"   value="导出到word"   class="notPrint">    
  <input   type="hidden"   name="out_excel"   onclick="AutomateExcel();"   value="导出到excel"   class="notPrint">    
   
  <title>浏览器表格导出到Excel代码</title>  
  <input   type="button"   name="out_word"   onclick="vbscript:buildDoc"   value="导出到word"   class="notPrint">    
  <input   type="button"   name="out_word1"   onclick="javascript:AutomateExcel()   "   value="导出到excel"   class="notPrint">    
   
   
  <table   id="data"   width="200"   border="1">  
      <tr>  
          <td>11</td>  
          <td>11</td>  
      </tr>  
      <tr>  
          <td>22</td>  
          <td>22</td>  
      </tr>  
      <tr>  
          <td>33</td>  
          <td>33</td>  
      </tr>  
      <tr>  
          <td>44 </td>  
          <td>44</td>  
      </tr>  
  </table>  
   
   
  <SCRIPT   LANGUAGE="JavaScript">  
  <!--  
  function   AutomateExcel()    
  {  
  //   Start   Excel   and   get   Application   object.  
  var   oXL   =   new   ActiveXObject("Excel.Application");    
  //   Get   a   new   workbook.  
  var   oWB   =   oXL.Workbooks.Add();  
  var   oSheet   =   oWB.ActiveSheet;  
  var   table   =   document.all.data;    
  var   hang   =   table.rows.length;  
   
  var   lie   =   table.rows(0).cells.length;    
   
  //   Add   table   headers   going   cell   by   cell.  
  for   (i=0;i<hang;i++)  
  {  
  for   (j=0;j<lie;j++)  
  {  
  oSheet.Cells(i+1,j+1).Value   =   table.rows(i).cells(j).innerText;  
  }  
   
  }  
  oXL.Visible   =   true;  
  oXL.UserControl   =   true;  
  }  
  //-->  
  </SCRIPT>  
   
  导出到Word代码  
  <script   language="vbscript">  
  Sub   buildDoc  
  set   table   =   document.all.data  
  row   =   table.rows.length  
  column   =   table.rows(1).cells.length  
   
  Set   objWordDoc   =   CreateObject("Word.Document")  
   
  'objWordDoc.Application.Documents.Add   theTemplate,   False  
  objWordDoc.Application.Visible=True  
   
  Dim   theArray(20,10000)  
  for   i=0   to   row-1  
  for   j=0   to   column-1  
  theArray(j+1,i+1)   =   table.rows(i).cells(j).innerTEXT  
  next  
  next  
  objWordDoc.Application.ActiveDocument.Paragraphs.Add.Range.InsertBefore("综合查询结果集")   //显示表格标题  
   
  objWordDoc.Application.ActiveDocument.Paragraphs.Add.Range.InsertBefore("")  
  Set   rngPara   =   objWordDoc.Application.ActiveDocument.Paragraphs(1).Range  
  With   rngPara  
  .Bold   =   True   //将标题设为粗体  
  .ParagraphFormat.Alignment   =   1   //将标题居中  
  .Font.Name   =   "隶书"   //设定标题字体  
  .Font.Size   =   18   //设定标题字体大小  
  End   With  
  Set   rngCurrent   =   objWordDoc.Application.ActiveDocument.Paragraphs(3).Range  
  Set   tabCurrent   =   ObjWordDoc.Application.ActiveDocument.Tables.Add(rngCurrent,row,column)  
   
  for   i   =   1   to   column    
   
  objWordDoc.Application.ActiveDocument.Tables(1).Rows(1).Cells(i).Range.InsertAfter   theArray(i,1)  
  objWordDoc.Application.ActiveDocument.Tables(1).Rows(1).Cells(i).Range.ParagraphFormat.alignment=1  
  next  
  For   i   =1   to   column  
  For   j   =   2   to   row    
  objWordDoc.Application.ActiveDocument.Tables(1).Rows(j).Cells(i).Range.InsertAfter   theArray(i,j)    
  objWordDoc.Application.ActiveDocument.Tables(1).Rows(j).Cells(i).Range.ParagraphFormat.alignment=1  
  Next  
  Next  
   
  End   Sub  
  </SCRIPT>  
  Top

6 楼hqcsx(蓝色心灵)回复于 2005-03-17 17:39:45 得分 0

<!--#include   file="../conn/conn.asp"-->  
  <%  
  '导出xml,csv,htm文件DEMO  
  '用法:ExportDB.asp?t=文件类型&sql=sql语句  
  '文件类型可以是htm,csv,xml,默认是htm  
  'http://www.51windows.Net  
  'tablename   =   "htm"  
  Dim   shijian,dingdan  
  shijian   =   now()  
  dingdan   =   year(shijian)&month(shijian)&day(shijian)&hour(shijian)&minute(shijian)&second(shijian)  
   
  dim   tablename  
  sql2   =   request("sql")  
  tablename   =   request("tablename")  
  if   tablename   =   ""   then  
  tablename   =   "数据导出结果"&dingdan  
  else  
  tablename   =   tablename&dingdan  
  end   if  
  sql   =   replace(sql2,";","")  
   
  filetype   =   lcase(request("t"))  
  dim   dbid,IndexPageName  
  function   HTMLEncode(fString)  
  if   not   isnull(fString)   then  
   
          fString   =   Server.HTMLEncode(fString)  
          fString   =   Replace(fString,   CHR(10)   &   CHR(10),   "</P><P>   ")  
          fString   =   Replace(fString,   CHR(10),   "<BR>   ")  
  fString   =   Replace(fString,   CHR(9),   "&nbsp;&nbsp;")  
   
          HTMLEncode   =   fString  
  end   if  
  end   function  
   
  function   Myreplace(str)  
  if   not   isnull(str)   then  
  fString   =   Replace(fString,"""",   """""")  
  Myreplace   =   str  
  else  
  Myreplace   =   ""  
  end   if  
  end   function  
   
  dim   def_export_val  
  def_export_sep   =   ","  
  def_export_val   =   """"  
  if   lcase(left(sql,6))<>"select"   then  
  Response.write   "出错了。。。"  
  Response.end  
  end   if  
  Set   rs   =   Conn.Execute(sql)  
  if   filetype="xml"   then  
  Response.contenttype="text/xml"  
  Response.Charset   =   "gb2312"  
  Response.AddHeader   "Content-Disposition",   "attachment;filename="&tablename&".xml"  
  Response.write   "<?xml   version=""1.0""   encoding=""gb2312""?>"  
  Response.write   vbnewline&"<root>"  
  strLine=""  
  dim   thefield(50)  
  i   =   0  
  For     each     x     in     rs.fields  
  thefield(i)=x.name  
  i=i+1  
  Next  
  While   rs.EOF   =false  
  strLine=   vbnewline&chr(9)&"<row>"  
  k=0  
  For     each     x     in     rs.fields    
  strLine=     strLine   &   vbnewline&chr(9)&chr(9)&"<"&thefield(k)&">"  
  if   instr(x.value,"<")   or   instr(x.value,">")   or   len(x.value)>255   then  
  strLine=     strLine   &"&lt;![CDATA["&   x.value   &"]]&gt;"  
  else  
  strLine=     strLine   &   x.value  
  end   if  
  strLine=     strLine   &"</"&thefield(k)&">"  
   
   
  k=k+1  
  Next  
  rs.MoveNext  
  Response.write   strLine   &vbnewline&   chr(9)&"</row>"  
  Wend  
  Response.write   vbnewline&"</root>"  
  elseif   filetype="csv"   then  
  'Response.contenttype="application/vnd.ms-excel"  
  Response.contenttype="text/csv"  
  Response.AddHeader   "Content-Disposition",   "attachment;filename="&tablename&".csv"  
  strLine=""  
  For     each     x     in     rs.fields  
  strLine=     strLine     &   def_export_val   &     x.name   &   def_export_val   &   def_export_sep  
  Next  
  Response.write   strLine   &   vbnewline  
  While   rs.EOF   =false  
  strLine=   ""  
  For     each     x     in     rs.fields  
  strLine=     strLine   &   def_export_val   &   trim(Myreplace(x.value))   &   def_export_val   &   def_export_sep  
  Next  
  rs.MoveNext  
  Response.write   strLine   &   vbnewline  
  Wend  
  else  
  if   filetype="htm"   then'弹出下载html的对话框  
  Response.contenttype="application/ms-download"  
  Response.AddHeader   "Content-Disposition",   "attachment;filename="&tablename&".htm"  
  end   if  
  %>  
  <html>  
  <head>  
  <meta   http-equiv="Content-Type"   content="text/html;   charset=gb2312">  
  <!--META   NAME="Author"   CONTENT="51windows,海娃,haiwa">  
  <META   NAME="Description"   CONTENT="Power   by   51windows.Net"-->  
  <title>导出数据。。</title>  
  <style>  
  <!--  
  body               {   font-family:   Verdana;   font-size:   11px   }  
  th                   {   font-family:   Verdana;   font-size:   11px;padding:3px;color:#FFFFFF;background-color:#999999;}  
  td                   {   font-family:   Verdana;   font-size:   11px;padding:3px;background-color:#EFEFEF;}  
  -->  
  </style>  
  </head>  
  <BODY   style="overflow:auto;"   topmargin=2   bgcolor=buttonface   oncontextmenu=self.event.returnValue=false   >  
  <div   align="center">  
  <table   border="0"   cellpadding="0"   cellspacing="1"   bgcolor="#000000"   width="100%">  
  <tr>  
  <%  
  i=0  
  For     each     x     in     rs.fields      
  strLine=     strLine     &chr(9)&chr(9)&"<th   align=""center"">"&     x.name     &"</th>"&   vbnewline  
  Next  
  Response.write   strLine&chr(9)&"</tr>"&   vbnewline   &   vbnewline  
  While   rs.EOF   =false  
  i=i+1  
  Response.write   chr(9)&"<tr>"&   vbnewline  
  strLine=   ""  
  For     each     x     in     rs.fields    
  strLine=     strLine     &chr(9)&chr(9)&"<td>"&     HTMLEncode(x.value)   &"</td>"&   vbnewline  
  Next  
  rs.MoveNext  
  Response.write   strLine  
  Response.write   chr(9)&"</tr>"&   vbnewline   &   vbnewline  
  Wend  
  Response.write   "</table>"&   vbnewline  
  if   filetype<>"htm"   and   filetype<>"xls"   and   filetype<>"txt"   then  
  Response.write   "<p   style='line-height:160%;'>"&i&"条记录   <a   href='?tablename="&request("tablename")&"&t=htm&sql="&server.urlencode(sql2)&"'>导出HTML</a>|<a   href='?tablename="&request("tablename")&"&t=csv&sql="&server.urlencode(sql2)&"'>导出EXCEL</a>|<a   href='?tablename="&request("tablename")&"&t=xml&sql="&server.urlencode(sql2)&"'>导出XML</a>"&   vbnewline  
  end   if  
  'Response.write   "<p>Power   by   <A   HREF=""http://www.51windows.Net""   target=""_blank"">51windows.Net</A>"&   vbnewline  
  Response.write   "</div>"&   vbnewline  
  Response.write   "</BODY>"&   vbnewline  
  Response.write   "</HTML>"&   vbnewline  
  %>  
   
  <%  
  end   if  
   
  rs.close  
  conn.close  
  Set   rs=nothing  
  Set   conn=nothing    
  %>  
  Top

相关问题

  • 页面保存
  • ****如何在web页面上嵌入excel,并在excel编辑,然后保存到服务器。*******
  • 请教:将页面导入到EXCEL并讲EXCEL文件在服务器端保存
  • 如何保存XML页面
  • 页面保存问题?
  • 如何用ASP实现把页面中的table导入到Excel?
  • 怎么把JSP页面上的TABLE输出到EXCEL表中?
  • 求助:JSP,页面表格的数据保存到本地,形成EXCEL表,如何实现?
  • 怎样将表(TABLE)和dbgrid的数据保存为EXCEL文件
  • excel保存问题

关键词

  • vbnewlineresponse
  • strline
  • chr
  • vbnewline
  • write
  • rs
  • and filetype

得分解答快速导航

  • 帖主:iamsatyr

相关链接

  • Web开发类图书

广告也精彩

反馈

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