关键代码:
<%
Response.Clear()
Response.CodePage=
Response.Charset="UTF-8"
Response.ContentType ="application/vnd.ms-word"
Response.AddHeader "Content-Disposition", "attachment; filename=WhitePaper"&formatDate(Now(),)&".doc"%><?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<?mso-application progid="Word.Document"?>
具体<w:wordDocument>的内容没写出来。可以通过将word另存为xml得到。
xml中动态数据部分通过数据查询,然后绑定字段求值即可。
默认生成的xml是三行代码,XML主体部分是第三行,千万不要去调整这一行的格式(换行、缩进)会导致生成的xml 使用Word打开报错。
另外,asp文件必须保存为(UTF-8 带签名)的。
在导出过程中用到的几个ASP自定义函数:
计算字符串中文字数:
function LenChStr(str)
dim i
c=
for i= to Len(str)
'if Asc(Mid(str,i,1)) < 0 then
'if CheckExp("^[^\u4E00-\u9FA5]+$", Mid(str,i,1))=True then
'If not (Asc(Mid(str, i, 1)) < 10000 And Asc(Mid(str, i, 1)) > -10000) Then
valAsc = Asc(Mid(str, i, ))
valAscW = AscW(Mid(str, i, ))
If valAsc <> valAscW Then
c=c+
end if
next LenChStr=c
end function
输出指定长度的字符串,不足则补位(中文算两个长度):
Function PadRight(Value,Length,sChar)
Dim strText,I
strText = String(Length,sChar)
strText = Value & strText if (len(Value)+lenChStr(Value))<Length then
PadRight = Left(strText,Length-LenChStr(Value))
else
PadRight=Value
end if
End Function
转换时间,时间格式化:
Function formatDate(t,ftype)
dim y, m, d, h, mi, s
formatDate=""
If IsDate(t)=False Then Exit Function
y=cstr(year(t))
m=cstr(month(t))
If len(m)= Then m="" & m
d=cstr(day(t))
If len(d)= Then d="" & d
h = cstr(hour(t))
If len(h)= Then h="" & h
mi = cstr(minute(t))
If len(mi)= Then mi="" & mi
s = cstr(second(t))
If len(s)= Then s="" & s
select case cint(ftype)
case
' yyyy-mm-dd
formatDate=y & "-" & m & "-" & d
case
' yy-mm-dd
formatDate=right(y,) & "-" & m & "-" & d
case
' mm-dd
formatDate=m & "-" & d
case
' yyyy-mm-dd hh:mm:ss
formatDate=y & "-" & m & "-" & d & " " & h & ":" & mi & ":" & s
case
' hh:mm:ss
formatDate=h & ":" & mi & ":" & s
case
' yyyy年mm月dd日
formatDate=y & "年" & m & "月" & d & "日"
case
' yyyymmdd
formatDate=y & m & d
case
'yyyymmddhhmmss
formatDate=y & m & d & h & mi & s
end select
End Function