• 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 

      

  • 相关阅读:
    虚拟机centos7下安装Python3.7
    笔试基础题
    虚拟环境的安装和配置
    记录记录每天的知识点和一些疑惑
    利用最小堆实现topk
    windows和虚拟机环境下的centos7进行文件传输
    vscode中命令行创建文件或文件夹|flask搭建环境的配置
    TS的点与区域的写法
    java——集合——Collections工具类——Collections集合工具类的方法——sort(List)
    java——集合——Set集合——可变参数
  • 原文地址:https://www.cnblogs.com/xuezhizhang/p/3252723.html
Copyright © 2020-2023  润新知