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

上一篇 / 下一篇  2008-03-25 11:09:51 / 个人分类:网络技术

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

 

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

 



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

TAG:

 

评分:0

我来说两句

显示全部

:loveliness: :handshake :victory: :funk: :time: :kiss: :call: :hug: :lol :'( :Q :L ;P :$ :P :o :@ :D :( :)

日历

« 2008-10-08  
   1234
567891011
12131415161718
19202122232425
262728293031 

我的存档

数据统计

  • 访问量: 4183
  • 日志数: 953
  • 建立时间: 2008-02-01
  • 更新时间: 2008-02-01

RSS订阅

Open Toolbar