zoukankan      html  css  js  c++  java
  • ASP导出Word带页眉页脚,中文不乱码

    关键代码:

    <%
    Response.Clear()
    Response.CodePage=65001
    Response.Charset="UTF-8"
    Response.ContentType ="application/vnd.ms-word"
    Response.AddHeader "Content-Disposition", "attachment; filename=WhitePaper"&formatDate(Now(),1)&".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=0
        for i=1 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, 1))
            valAscW = AscW(Mid(str, i, 1))
            If valAsc <> valAscW Then
                c=c+1
            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)=1 Then m="0" & m 
    d=cstr(day(t)) 
    If len(d)=1 Then d="0" & d 
    h = cstr(hour(t)) 
    If len(h)=1 Then h="0" & h 
    mi = cstr(minute(t)) 
    If len(mi)=1 Then mi="0" & mi 
    s = cstr(second(t)) 
    If len(s)=1 Then s="0" & s 
    select case cint(ftype) 
    case 1 
    ' yyyy-mm-dd 
    formatDate=y & "-" & m & "-" & d 
    case 2 
    ' yy-mm-dd 
    formatDate=right(y,2) & "-" & m & "-" & d 
    case 3 
    ' mm-dd 
    formatDate=m & "-" & d 
    case 4 
    ' yyyy-mm-dd hh:mm:ss 
    formatDate=y & "-" & m & "-" & d & " " & h & ":" & mi & ":" & s 
    case 5 
    ' hh:mm:ss 
    formatDate=h & ":" & mi & ":" & s 
    case 6 
    ' yyyy年mm月dd日 
    formatDate=y & "" & m & "" & d & "" 
    case 7 
    ' yyyymmdd 
    formatDate=y & m & d 
    case 8 
    'yyyymmddhhmmss 
    formatDate=y & m & d & h & mi & s 
    end select 
    End Function 

      

  • 相关阅读:
    一个空类会生成哪些默认函数
    What is VMR(Video Mixing Render)From MSDN
    DirectX backface culling(背面剔除)
    D3DPOOL(资源池)
    两道概率题供大家周末把玩
    空间两点间的距离
    n != n, n == n
    C++默认参数
    D3DPT_TRIANGLELIST与D3DPT_TRIANGLESTRIP
    D3D中的设备丢失
  • 原文地址:https://www.cnblogs.com/xuezhizhang/p/3252723.html
Copyright © 2011-2022 走看看