记录:asp导出excel,并且格式化数值为文本

今天做asp导出excel的时候,发现如果数值很大的话,excel会自动科学计数,手动变为文本后,excel自动把后面的尾数全部换成0000,这样的话在实际中就有很多问题。网上查了一下解决方式。记录之:

 

ASP/Visual Basic代码
  1. <!--#include file="top.asp" --&gt   
  2.  "750" border="0" cellspacing="0" cellpadding="0" align="center">   
  3.       
  4.      "center">   
  5.      

    点击这里生成excel 

      
  6.       
  7.  
  8.  if request("act")="" then     
  9. 'response.write ("生成excel文件")     
  10.  else   
  11. 'dim conn,strconn     
  12. 'strconn=driver=;server=wen;uid=sa;pwd=;database=db_test     
  13. 'set  conn=server.createobject("adodb.connection")     
  14. 'conn.open strconn   
  15. dim  rs,sql,filename,fs,myfile,x   
  16. set fs=server.createobject("scripting.filesystemobject")     
  17. '--假设你想让生成的excel文件做如下的存放     
  18. filename = server.mappath("data.xls")     
  19. '--如果原来的excel文件存在的话删除它     
  20. if fs.fileexists(filename) then   
  21.  fs.deletefile(filename)    
  22.  'response.write "dele"    
  23. end if   
  24. '--创建excel文件   
  25. set myfile = fs.createtextfile(filename,true)   
  26. set rs = server.createobject("adodb.recordset")   
  27. '--从数据库中把你想放到excel中的数据查出来     
  28. sql = "Select mid(zgbm_no,1,6) as dzdm,dwmc as x01,xmmc as x02,frdm as a05,jsdz_bm as a03,v4 as x04,v5 as x05,v6 as x06,v7 as x07,v8 as x08,v9 as x09,v10_1 as x10,v11_1 as x11,v12 as x12,v13 as x13,v14 as x14,v15 as x15,v16 as x16,v17 as x17,v18 as x18,v19 as x19,dwfzr as xdw,tjfzr as xtj,tbr as xtb,rq as xrq FROM xmzb where zgbm_no like '"+session("zgbm_no")+"%' order by id desc"  
  29. 'response.write sql   
  30. rs.open sql,conn,1,1   
  31. if rs.eof and rs.bof then   
  32. else   
  33.  dim strline,responsestr     
  34.  strline=""  
  35.  for each x in  rs.fields   
  36. strline = strline & x.name & chr(9)   
  37.  next   
  38.  '--将表的列名先写入excel     
  39.  myfile.writeline strline   
  40.  do while not rs.eof     
  41. strline=""  
  42. for each x in  rs.fields    
  43.   strline= strline & "="""&x.value """"& chr(9)    
  44.  'if x<>"" then   
  45.  'strline = strline & cstr(x.value) & chr(9)     
  46.  'else   
  47.  'strline = strline & x.value & chr(9)   
  48.  'end if   
  49. next   
  50. '--将表的数据写入excel     
  51. myfile.writeline strline   
  52. rs.movenext   
  53.  loop   
  54. end if   
  55. response.write "生成成功!请  "+""+"点击这里下载"+""  
  56. rs.close     
  57. set  rs  =  nothing   
  58.   end if   
  59. %>    
  60.          
  61.          
  62.      
  63.   
  64.   

 其中:strline= strline & "="""&x.value """"& chr(9)  就是把数值转换成文本文件。

其中top文件是数据库联接文件。内容如下:

ASP/Visual Basic代码
  1. ConnectString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("access.mdb")   
  2. Set conn = Server.CreateObject("ADODB.Connection")   
  3. conn.open ConnectString   
  4. %>  

 



Link URL: http://www.csjrc.com/default.asp?id=89

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/89703/viewspace-217123/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/89703/viewspace-217123/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值