发新话题
打印

ASP按定制格式导出word文件例子

ASP按定制格式导出word文件例子

注意:该函数是我在做一个学生成绩管理系统时作得,当用户点击了“导出到word文件”后的执行过程,由于数据库设置的原因,因此需要单独取出用户所在的系,班级,学号
另外这个实现的思路实际是fso生成文件的过程,然后把定制好的word文件保存为html文件,再编辑此html文件,在适当的位置加入动态数据,最后按照规则循环出成绩列表即可
生成的文件名可以任意定义,但是生成文件后必须要用word打开,打开后的文件将是按照您预先定制的格式导出的word文件。
此教程绝无仅有,强烈要求加入FAQ.^0^Sub oput()
did = Trim(Request("did"))
gid = Trim(Request("gid"))
userid = Trim(Request("userid"))
intYears = Trim(Request("intYears"))
If intYears="" Then intYears=1
If userid="" Or IsNumeric(userid)=False Or did="" Or IsNumeric(did)=False Or gid="" Or IsNumeric(gid)=False Then Call Err("","对不起,获取参数错误!")
If intYears="" Then
intYears=1
strCodes=" and intYear="&intYears&""
Elseif intYears=7 then
strCodes=""
Else
strCodes=" and intYear="&intYears&""
End if
Dim objRS,SaveName,objExcel,ExcelPath,fso,MyFile,strchengji,strname,strxueqi,strkecheng
Set objRS = Server.CreateObject("Adodb.Recordset")objRs.Open "Select name from S_Grade where id="&gid&"",Conn,1,1
if objRS.eof then  Call Err("","对不起,获取班级出错了,操作被终止。")
banji=objrs("name")
objrs.closeobjRs.Open "Select name from S_Department where id="&Did&"",Conn,1,1
if objRS.eof then  Call Err("","对不起,获取系部信息出错了,操作被终止。")
xibu=objrs("name")
objrs.closeobjRs.Open "Select snum from S_User where id="&Userid&"",Conn,1,1
if objRS.eof then  Call Err("","对不起,获取学生信息出错了,操作被终止。")
xuesheng=objrs("snum")
objrs.closeobjRS.Open "Select sname,Cname,Score,intYear From S_Cs Where Sid="&Userid&" and did="&did&" and gid="&gid&strCodes&" order by intYear,Cid",Conn,1,1
if objRS.eof then  Call Err("","对不起,获取成绩出错了,操作被终止。")Set fso = CreateObject("Scripting.FileSystemObject")
sFileName = StrCreatFileName
Set MyFile = fso.CreateTextFile(Server.MapPath(".") & "\"&sFileName, True)
myString="<html xmlns:o=""urn:schemas-microsoft-com:office:office"""&vbcrlf
myString=myString&"xmlns:w=""urn:schemas-microsoft-com:office:word"""&vbcrlf
myString=myString&"xmlns=""http://www.w3.org/TR/REC-html40"">"&vbcrlf
myString=myString&"<head>"&vbcrlf
myString=myString&"<meta http-equiv=Content-Type content=""text/html; charset=GB2312"">"&vbcrlf
myString=myString&"<meta name=ProgId content=Word.Document>"&vbcrlf
myString=myString&"<meta name=Generator content=""Microsoft Word 9"">"&vbcrlf
myString=myString&"<meta name=Originator content=""Microsoft Word 9"">"&vbcrlf
myString=myString&"<link rel=File-List href=""./姓名.files/filelist.xml"">"&vbcrlf
myString=myString&"<title>潍坊职业学院成绩单</title>"&vbcrlf
myString=myString&"<!--[if gte mso 9]><xml>"&vbcrlf
myString=myString&"<o:DocumentProperties>"&vbcrlf
myString=myString&"<o:Author>yt</o:Author>"&vbcrlf
myString=myString&"<o:LastAuthor>Master</o:LastAuthor>"&vbcrlf
myString=myString&"<o:Revision>2</o:Revision>"&vbcrlf
myString=myString&"<o:TotalTime>71</o:TotalTime>"&vbcrlf
myString=myString&"<o:Created>2006-04-05T11:09:00Z</o:Created>"&vbcrlf
myString=myString&"<o:LastSaved>2006-04-05T11:09:00Z</o:LastSaved>"&vbcrlf
myString=myString&"<o:Pages>1</o:Pages>"&vbcrlf
myString=myString&"<o:Words>11</o:Words>"&vbcrlf
myString=myString&"<o:Characters>63</o:Characters>"&vbcrlf
myString=myString&"<o:Lines>1</o:Lines>"&vbcrlf
myString=myString&"<o:Paragraphs>1</o:Paragraphs>"&vbcrlf
myString=myString&"<o:CharactersWithSpaces>77</o:CharactersWithSpaces>"&vbcrlf
myString=myString&"<o:Version>9.2812</o:Version>"&vbcrlf
myString=myString&"</o:DocumentProperties>"&vbcrlf
myString=myString&"</xml><![endif]--><!--[if gte mso 9]><xml>"&vbcrlf
myString=myString&"<w:WordDocument>"&vbcrlf
myString=myString&"<w:PunctuationKerning/>"&vbcrlf
myString=myString&"<w:DrawingGridVerticalSpacing>7.8 磅</w:DrawingGridVerticalSpacing>"&vbcrlf
myString=myString&"<w:DisplayHorizontalDrawingGridEvery>0</w:DisplayHorizontalDrawingGridEvery>"&vbcrlf
myString=myString&"<w:DisplayVerticalDrawingGridEvery>2</w:DisplayVerticalDrawingGridEvery>"&vbcrlf
myString=myString&"<w:Compatibility>"&vbcrlf
myString=myString&"<w:SpaceForUL/>"&vbcrlf
myString=myString&"<w:BalanceSingleByteDoubleByteWidth/>"&vbcrlf
myString=myString&"<w:DoNotLeaveBackslashAlone/>"&vbcrlf
myString=myString&"<w:ULTrailSpace/>"&vbcrlf
myString=myString&"<w:DoNotExpandShiftReturn/>"&vbcrlf
myString=myString&"<w:AdjustLineHeightInTable/>"&vbcrlf
myString=myString&"<w:UseFELayout/>"&vbcrlf
myString=myString&"</w:Compatibility>"&vbcrlf
myString=myString&"</w:WordDocument>"&vbcrlf
myString=myString&"</xml><![endif]-->"&vbcrlf
myString=myString&"<style>"&vbcrlf
myString=myString&"<!--"&vbcrlf
myString=myString&"/* Font Definitions */"&vbcrlf
myString=myString&"@font-face"&vbcrlf
myString=myString&"{font-family:宋体;"&vbcrlf
myString=myString&"panose-1:2 1 6 0 3 1 1 1 1 1;"&vbcrlf
myString=myString&"mso-font-alt:SimSun;"&vbcrlf
myString=myString&"mso-font-charset:134;"&vbcrlf
myString=myString&"mso-generic-font-family:auto;"&vbcrlf
myString=myString&"mso-font-pitch:variable;"&vbcrlf
myString=myString&"mso-font-signature:3 135135232 16 0 262145 0;}"&vbcrlf
myString=myString&"@font-face"&vbcrlf
myString=myString&"{font-family:""\@宋体"";"&vbcrlf
myString=myString&"panose-1:2 1 6 0 3 1 1 1 1 1;"&vbcrlf
myString=myString&"mso-font-charset:134;"&vbcrlf
myString=myString&"mso-generic-font-family:auto;"&vbcrlf
myString=myString&"mso-font-pitch:variable;"&vbcrlf
myString=myString&"mso-font-signature:3 135135232 16 0 262145 0;}"&vbcrlf
myString=myString&"/* Style Definitions */"&vbcrlf
myString=myString&"p.MsoNormal, li.MsoNormal, div.MsoNormal"&vbcrlf
myString=myString&"{mso-style-parent:"""";"&vbcrlf
myString=myString&"margin:0cm;"&vbcrlf
myString=myString&"margin-bottom:.0001pt;"&vbcrlf
myString=myString&"text-align:justify;"&vbcrlf
myString=myString&"text-justify:inter-ideograph;"&vbcrlf
myString=myString&"mso-pagination:none;"&vbcrlf
myString=myString&"font-size:10.5pt;"&vbcrlf
myString=myString&"mso-bidi-font-size:12.0pt;"&vbcrlf
myString=myString&"font-family:""Times New Roman"";"&vbcrlf
myString=myString&"mso-fareast-font-family:宋体;"&vbcrlf
myString=myString&"mso-font-kerning:1.0pt;}"&vbcrlf
myString=myString&"/* Page Definitions */"&vbcrlf
myString=myString&"@page"&vbcrlf
myString=myString&"{mso-page-border-surround-header:no;"&vbcrlf
myString=myString&"mso-page-border-surround-footer:no;}"&vbcrlf
myString=myString&"@page Section1"&vbcrlf
myString=myString&"{size:595.3pt 841.9pt;"&vbcrlf
myString=myString&"margin:72.0pt 37.3pt 72.0pt 45.0pt;"&vbcrlf
myString=myString&"mso-header-margin:42.55pt;"&vbcrlf
myString=myString&"mso-footer-margin:49.6pt;"&vbcrlf
myString=myString&"mso-paper-source:0;"&vbcrlf
myString=myString&"layout-grid:15.6pt;}"&vbcrlf
myString=myString&"div.Section1"&vbcrlf
myString=myString&"{page:Section1;}"&vbcrlf
myString=myString&"/* List Definitions */"&vbcrlf
myString=myString&"@list l0"&vbcrlf
myString=myString&"{mso-list-id:347021429;"&vbcrlf
myString=myString&"mso-list-type:hybrid;"&vbcrlf
myString=myString&"mso-list-template-ids:565234532 -927567266 67698713 67698715 67698703 67698713 67698715 67698703 67698713 67698715;}"&vbcrlf
myString=myString&"@list l0:level1"&vbcrlf
myString=myString&"{mso-level-text:%1.;"&vbcrlf
myString=myString&"mso-level-tab-stop:18.0pt;"&vbcrlf
myString=myString&"mso-level-number-position:left;"&vbcrlf
myString=myString&"margin-left:18.0pt;"&vbcrlf
myString=myString&"text-indent:-18.0pt;}"&vbcrlf
myString=myString&"ol"&vbcrlf
myString=myString&"{margin-bottom:0cm;}"&vbcrlf
myString=myString&"ul"&vbcrlf
myString=myString&"{margin-bottom:0cm;}"&vbcrlf
myString=myString&"-->"&vbcrlf
myString=myString&"</style>"&vbcrlf
myString=myString&"</head>"&vbcrlf
'//开始
myString=myString&"<body lang=ZH-CN style='tab-interval:21.0pt;text-justify-trim:punctuation'>"&vbcrlf
myString=myString&"<div class=Section1 style='layout-grid:15.6pt' align=center>"&vbcrlf
myString=myString&"<p class=MsoNormal style='margin-left:-.86gd;text-indent:-.05pt'><span"&vbcrlf
myString=myString&"lang=EN-US><![if !supportEmptyParas]> <![endif]><o:p></o:p></span></p>"&vbcrlf
myString=myString&"<table border=1 cellspacing=0 cellpadding=0 style='margin-left:-3.6pt;"&vbcrlf
myString=myString&"border-collapse:collapse;border:none;mso-border-alt:solid windowtext .5pt;"&vbcrlf
myString=myString&"mso-padding-alt:0cm 5.4pt 0cm 5.4pt'>"&vbcrlf
myString=myString&"<tr style='height:13.25pt'>"&vbcrlf
myString=myString&"<td width=684 valign=top style='width:513.0pt;border:solid windowtext .5pt;"&vbcrlf
myString=myString&"padding:0cm 5.4pt 0cm 5.4pt;height:13.25pt'>"&vbcrlf
myString=myString&"<p class=MsoNormal align=center style='text-align:center'><b><span"&vbcrlf
myString=myString&"style='font-size:14.0pt;mso-bidi-font-size:12.0pt;font-family:宋体;mso-ascii-font-family:"&vbcrlf
myString=myString&"""Times New Roman"";mso-hansi-font-family:""Times New Roman""'>潍坊职业学院成绩单</span></b><b><span"&vbcrlf
myString=myString&"lang=EN-US style='font-size:14.0pt;mso-bidi-font-size:12.0pt'><o:p></o:p></span></b></p>"&vbcrlf
myString=myString&"<p class=MsoNormal><span style='font-family:宋体;mso-ascii-font-family:""Times New Roman"";"&vbcrlf
myString=myString&"mso-hansi-font-family:""Times New Roman""'>所在系部:</span><span lang=EN-US><span"&vbcrlf
myString=myString&"style=""mso-spacerun: yes"">"&xibu&"     </span></span><span"&vbcrlf
myString=myString&"style='font-family:宋体;mso-ascii-font-family:""Times New Roman"";mso-hansi-font-family:"&vbcrlf
myString=myString&"""Times New Roman""'>班级:</span><span lang=EN-US><span style=""mso-spacerun:"&vbcrlf
myString=myString&"yes"">"&banji&"     </span></span><span style='font-family:宋体;"&vbcrlf
myString=myString&"mso-ascii-font-family:""Times New Roman"";mso-hansi-font-family:""Times New Roman""'>姓名:</span><span"&vbcrlf
myString=myString&"lang=EN-US><span style=""mso-spacerun: yes"">"&objrs("sname")&"     </span></span><span"&vbcrlf
myString=myString&"style='font-family:宋体;mso-ascii-font-family:""Times New Roman"";mso-hansi-font-family:"&vbcrlf
myString=myString&"""Times New Roman""'>学号:"&xuesheng&"</span></p>"&vbcrlf
myString=myString&"</td>"&vbcrlf
myString=myString&"</tr>"&vbcrlf
myString=myString&"<tr style='height:5.7pt'>"&vbcrlf
myString=myString&"<td width=684 valign=top style='width:513.0pt;border:solid windowtext .5pt;"&vbcrlf
myString=myString&"border-top:none;mso-border-top-alt:solid windowtext .5pt;padding:0cm 5.4pt 0cm 5.4pt;"&vbcrlf
myString=myString&"height:5.7pt'>"&vbcrlf
myString=myString&"<table border=1 cellspacing=0 cellpadding=0 align=left style='border-collapse:"&vbcrlf
myString=myString&"collapse;border:none;mso-border-alt:solid windowtext .5pt;mso-table-overlap:"&vbcrlf
myString=myString&"never;mso-table-lspace:9.0pt;mso-table-rspace:9.0pt;mso-table-anchor-vertical:"&vbcrlf
myString=myString&"paragraph;mso-table-anchor-horizontal:margin;mso-table-left:left;mso-table-top:"&vbcrlf
myString=myString&"6.65pt;mso-padding-alt:0cm 5.4pt 0cm 5.4pt'>"&vbcrlfmyString=myString&"<tr style='height:2.45pt'>"&vbcrlf
myString=myString&"<td width=177 valign=top style='width:114.75pt;border:solid windowtext .5pt;padding:0cm 5.4pt 0cm 5.4pt;height:2.45pt'>"&vbcrlf
myString=myString&"<p class=MsoNormal align=center style='text-align:center'><span style='font-family:宋体;mso-ascii-font-family:""Times New Roman"";mso-hansi-font-family:""Times New Roman""'>学生姓名</span></p></td>"&vbcrlf'//myString=myString&"<td width=177 valign=top style='width:123.85pt;border:solid windowtext .5pt;border-left:none;mso-border-left-alt:solid windowtext .5pt;padding:0cm 5.4pt 0cm 5.4pt;height:2.45pt'>"&vbcrlf
myString=myString&"<p class=MsoNormal align=center style='text-align:center'><span style='font-family:宋体;mso-ascii-font-family:""Times New Roman"";mso-hansi-font-family:""Times New Roman""'>课程名称</span></p></td>"&vbcrlfmyString=myString&"<td width=177 valign=top style='width:132.7pt;border:solid windowtext .5pt;border-left:none;mso-border-left-alt:solid windowtext .5pt;padding:0cm 5.4pt 0cm 5.4pt;height:2.45pt'>"&vbcrlf
myString=myString&"<p class=MsoNormal align=center style='text-align:center'><span style='font-family:宋体;mso-ascii-font-family:""Times New Roman"";mso-hansi-font-family:""Times New Roman""'>考试成绩</span></p></td>"&vbcrlfmyString=myString&"<td width=177 valign=top style='width:123.4pt;border:solid windowtext .5pt;  border-left:none;mso-border-left-alt:solid windowtext .5pt;padding:0cm 5.4pt 0cm 5.4pt; height:2.45pt'>"&vbcrlf
myString=myString&"<p class=MsoNormal align=center style='text-align:center'><span style='font-family:宋体;mso-ascii-font-family:""Times New Roman"";mso-hansi-font-family: ""Times New Roman""'>所属学期</span></p></td></tr>"&vbcrlf
'//开始循环For i=0 To objRs.recordcount
If objRs.eof or objRs.bof Then Exit Forstrchengji=objRs("Score")
If strchengji<60 Then
strchengji="<font color=red><b>"&strchengji&"</b></font>分"
Else
strchengji="<font color=green><b>"&strchengji&"</b></font>分"
End if
strname=objRs("Sname")
strxueqi=objRs("intYear")
strkecheng=objRs("Cname")myString=myString&"<tr style='height:2.3pt'>"&vbcrlf
myString=myString&"<td width=177 valign=top style='width:114.75pt;border:solid windowtext .5pt; border-top:none;mso-border-top-alt:solid windowtext .5pt;padding:0cm 5.4pt 0cm 5.4pt;height:2.3pt'><p class=MsoNormal align=center style='text-align:center'><![if !supportEmptyParas]> <![endif]><span lang=EN-US><o:p>"&strname&"</o:p></span></p></td>"&vbcrlf
myString=myString&"<td width=177 valign=top style='width:123.85pt;border-top:none;border-left:none;border-bottom:solid windowtext .5pt;border-right:solid windowtext .5pt;mso-border-top-alt:solid windowtext .5pt;mso-border-left-alt:solid windowtext .5pt;padding:0cm 5.4pt 0cm 5.4pt;height:2.3pt'><p class=MsoNormal align=center style='text-align:center'><![if !supportEmptyParas]> <![endif]><span lang=EN-US><o:p>"&strkecheng&"</o:p></span></p></td>"&vbcrlf
myString=myString&"<td width=177 valign=top style='width:132.7pt;border-top:none;border-left:none;border-bottom:solid windowtext .5pt;border-right:solid windowtext .5pt;mso-border-top-alt:solid windowtext .5pt;mso-border-left-alt:solid windowtext .5pt;padding:0cm 5.4pt 0cm 5.4pt;height:2.3pt'><p class=MsoNormal align=center style='text-align:center'><![if !supportEmptyParas]> <![endif]><span lang=EN-US><o:p>"&strchengji&"</o:p></span></p></td>"&vbcrlf
myString=myString&"<td width=177 valign=top style='width:123.4pt;border-top:none;border-left:none;border-bottom:solid windowtext .5pt;border-right:solid windowtext .5pt;mso-border-top-alt:solid windowtext .5pt;mso-border-left-alt:solid windowtext .5pt;padding:0cm 5.4pt 0cm 5.4pt;height:2.3pt'><p class=MsoNormal align=center style='text-align:center'><![if !supportEmptyParas]> <![endif]><span lang=EN-US><o:p>第"&strxueqi&"学期</o:p></span></p></td>"&vbcrlf
myString=myString&"</tr>"&vbcrlf
objRs.movenext
Next
'//循环结束myString=myString&"</table><p class=MsoNormal><b><span lang=EN-US style='font-size:14.0pt;mso-bidi-font-size:  12.0pt'><o:p></o:p></span></b></p>  </td> </tr> <tr style='height:20.45pt'>"&vbcrlf
myString=myString&"<td width=684 valign=top style='width:513.0pt;border:solid windowtext .5pt;"&vbcrlf
myString=myString&"border-top:none;mso-border-top-alt:solid windowtext .5pt;padding:0cm 5.4pt 0cm 5.4pt;  height:20.45pt'>"&vbcrlf
myString=myString&"<p class=MsoNormal><span style='font-family:宋体;mso-ascii-font-family:""Times New Roman"";  mso-hansi-font-family:""Times New Roman""'>日期:"&now&"</span></p>  </td> </tr></table>"&vbcrlf
myString=myString&"</div></body></html>"&vbcrlf
   MyFile.WriteLine(myString)
   objRs.close
set objRs=nothing
   MyFile.Close
set fso=nothing%>
<script language="javascript">
if (confirm("恭喜!\n\n该生的成绩已经被成功导入到Word文件中了!\n\n点击“确定”下载该文档,点击“取消”返回该生的成绩列表!\n\n??<%=CompanyName%>"))
  location.href="?action=download";
else
  location.href="javascript:history.back(1)";
</script>
<%End Sub%>

TOP

发新话题