蒙国造博客

ASP生成csv文件代码

ASP生成csv文件代码,如下:

' 函数定义
Sub WriteCsv(FileName, Content)  
    Path =server.MapPath(FileName)
    set stm=server.CreateObject("ADODB.stream")
    stm.Type=2 
    stm.mode=3
    stm.charset="utf-8"
    stm.open
    stm.WriteText Content,1
    stm.SaveToFile Path,2 
    stm.flush
    stm.Close
    set stm=nothing
End Sub

' 设置文件名和内容
FileName = "out.csv"
Content = "1,2,3,4,5" &vbCrLf
Content = Content & "6,7,8,9,10"

' 调用生成函数
Call WriteCsv(FileName, Content)

以下是可选的代码,用以生成之后直接弹出下载框,可以不用:

    Response.Clear 
    Response.charset = "utf-8"
    Response.AddHeader "content-disposition", "attachment; filename=" & FileName 
    Response.ContentType = "application/octet-stream" 
    Response.buffer = TRUE 
    Set Stream = server.CreateObject("ADODB.Stream") 
    Stream.Open 
    Stream.charset="UTF-8"
    Stream.Type=1 
    Stream.LoadFromFile Path
    While Not Stream.EOS 
    Response.BinaryWrite Stream.Read(1024 * 64) 
    Wend 
    Stream.Close 
    Set Stream = Nothing 
    Response.Flush 
    Response.End

参考资料:http://bbs.csdn.net/topics/300126977

退出移动版