ASP将数据库数据生成word文档

发布时间:2007年07月08日      浏览次数:2689 次
<%
Response.Buffer = TRUE
Response.ContentType = "application/msword" '--word
Set conn = CreateObject("Adodb.Connection")
strConn = "Driver={Microsoft Access Driver (*.mdb)};dbq=" & Server.Mappath("1.mdb")
conn.Open strConn
Set rs = Server.CreateObject("ADODB.Recordset")
sql="select * from usr "
rs.open sql,conn,3,3
if rs.eof and rs.bof then
end if
Dim fso, MyFile
Set fso = CreateObject("Scripting.FileSystemObject") '用到fso

Set MyFile = fso.CreateTextFile(Server.MapPath(".") & "\word\temp.doc", True) '文件放在当前目录的word/下
myString="<html xmlns:o=““urn:schemas-microsoft-com:office:office”“ xmlns:w=““urn:schemas-microsoft-com:office:word”“ xmlns=““http://www.w3.org/TR/REC-html40”“> <head> <meta http-equiv=Content-Type content=““text/html; charset=GB2312”“> <meta name=ProgId content=Word.Document> <meta name=Generator content=““Microsoft Word 9”“> <meta name=Originator content=““Microsoft Word 9”“> <!--[if gte mso 9]><xml> <o:DocumentProperties> <o:Author>esun</o:Author> <o:LastAuthor>esun</o:LastAuthor> <o:Revision>1</o:Revision> <o:TotalTime>1</o:TotalTime> <o:Created>2005-02-02T03:07:00Z</o:Created> <o:LastSaved>2005-02-02T03:08:00Z</o:LastSaved> <o:Pages>1</o:Pages> <o:Company>Microsoft</o:Company> <o:Lines>1</o:Lines> <o:Paragraphs>1</o:Paragraphs> <o:Version>9.2812</o:Version> </o:DocumentProperties> </xml><![endif]--><!--[if gte mso 9]><xml> <w:WordDocument> <w:View>Print</w:View> <w:PunctuationKerning/> <w:DrawingGridVerticalSpacing>7.8 磅</w:DrawingGridVerticalSpacing> <w:DisplayHorizontalDrawingGridEvery>0</w:DisplayHorizontalDrawingGridEvery> <w:DisplayVerticalDrawingGridEvery>2</w:DisplayVerticalDrawingGridEvery> <w:Compatibility> <w:SpaceForUL/> <w:BalanceSingleByteDoubleByteWidth/> <w:DoNotLeaveBackslashAlone/> <w:ULTrailSpace/> <w:DoNotExpandShiftReturn/> <w:AdjustLineHeightInTable/> <w:UseFELayout/> </w:Compatibility> </w:WordDocument> </xml><![endif]--> <style> <!-- /* Font Definitions */ @font-face       {font-family:宋体;       panose-1:2 1 6 0 3 1 1 1 1 1;       mso-font-alt:SimSun;       mso-font-charset:134;       mso-generic-font-family:auto;       mso-font-pitch:variable;       mso-font-signature:3 135135232 16 0 262145 0;} @font-face       {font-family:”“\@宋体”“;       panose-1:2 1 6 0 3 1 1 1 1 1;       mso-font-charset:134;       mso-generic-font-family:auto;       mso-font-pitch:variable;       mso-font-signature:3 135135232 16 0 262145 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal       {mso-style-parent:”“““;       margin:0cm;       margin-bottom:.0001pt;       text-align:justify;       text-justify:inter-ideograph;       mso-pagination:none;       font-size:10.5pt;       mso-bidi-font-size:12.0pt;       font-family:”“Times New Roman”“;       mso-fareast-font-family:宋体;       mso-font-kerning:1.0pt;} /* Page Definitions */ @page       {mso-page-border-surround-header:no;       mso-page-border-surround-footer:no;} @page Section1       {size:595.3pt 841.9pt;       margin:72.0pt 90.0pt 72.0pt 90.0pt;       mso-header-margin:42.55pt;       mso-footer-margin:49.6pt;       mso-paper-source:0;       layout-grid:15.6pt;} div.Section1       {page:Section1;} --> </style> </head> <body lang=ZH-CN style='tab-interval:21.0pt;text-justify-trim:punctuation'> <div class=Section1 style='layout-grid:15.6pt'> <table border=1 cellspacing=0 cellpadding=0 align='center' style='border-collapse:collapse; border:none;mso-border-alt:solid windowtext .5pt;mso-padding-alt:0cm 5.4pt 0cm 5.4pt'> <tr> <td width=284 valign=top style='width:213.05pt;border:solid windowtext .5pt; padding:0cm 5.4pt 0cm 5.4pt'> <p class=MsoNormal><span style='font-family:宋体;mso-ascii-font-family:”“Times New Roman”“; mso-hansi-font-family:”“Times New Roman”“;color:red'>数据ID</span></p> </td> <td width=284 valign=top style='width:213.05pt;border:solid windowtext .5pt; border-left:none;mso-border-left-alt:solid windowtext .5pt;padding:0cm 5.4pt 0cm 5.4pt'> <p class=MsoNormal><span style='font-family:宋体;mso-ascii-font-family:”“Times New Roman”“; mso-hansi-font-family:”“Times New Roman”“;color:red'>数据值</span></p> </td> </tr>"
while not rs.eof and not rs.bof
myString=myString&"<tr> <td width=284 valign=top style='width:213.05pt;border:solid windowtext .5pt; border-top:none;mso-border-top-alt:solid windowtext .5pt;padding:0cm 5.4pt 0cm 5.4pt'> <p class=MsoNormal><span style='font-family:宋体;mso-ascii-font-family:”“Times New Roman”“; mso-hansi-font-family:”“Times New Roman”“'>"&rs("id")&"</span></p> </td> <td width=284 valign=top style='width:213.05pt;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'> <p class=MsoNormal><span style='font-family:宋体;mso-ascii-font-family:”“Times New Roman”“; mso-hansi-font-family:”“Times New Roman”“'>"&rs("usrname")&"</span></p> </td> </tr>"
rs.movenext
wend
myString=myString&"</table> <p class=MsoNormal><span lang=EN-US><![if !supportEmptyParas]> <![endif]><o:p></o:p></span></p> </div> </body> </html> "
MyFile.WriteLine(myString)
MyFile.Close '关闭文件
Dim strFilePath
Const adTypeBinary = 1
strFilePath = "word\temp.doc"
Set objStream = Server.CreateObject("ADODB.Stream")
objStream.Open
objStream.Type = adTypeBinary
objStream.LoadFromFile Server.MapPath(strFilePath) 'change the path if ecessary
Response.ContentType = "application/octet-stream"
'Response.AddHeader "Content-Disposition", "attachment; filename=asp生成的word文档.doc"'此代码是以文件下载方式打开
Response.AddHeader "Content-Disposition", " filename=asp生成的word文档.doc"'此代码是以IE浏览器打开,即将上面的代码去掉“attachment;”即可
'发现把attachment去掉,就会用ie直接打开了。不让它直接打开!
'如果文件名固定,那就直接filename=文件名.doc,这个名字就是用户选“保存”时,出现的名字
Response.BinaryWrite objStream.Read
Response.Flush
objStream.Close
Set objStream = Nothing '写完,释放对象
'以下为从库中取资料并生成Wrod代码:
rs.Close
Set rs=Nothing
conn.Close
Set conn = Nothing
'注:myString="" 里面到底是什么?
'用word做一个你要的格式的文档,都排好了,
'把要填的地方做好标记,比如写几个字什么的。
'然后“另存为”web页面。本文所说的是文档里没有图片的情况,有的话我也不会。
'到你的硬盘里找到这个web页面,打开,查看源文件,ctrl+a,ctrl+c,
'在word里,ctrl+N,ctrl+v,ctrl+F,点“替换”标签,
'第一步:查找内容填双引号",替换为填两个双引号"",全部替换即可。
'第二步:点“高级”,查找内容里填“特殊字符”的“段落标记”,替换为填“特殊字符”的“不间断空格”,全部替换即可。
'然后ctrl+a,ctrl+c,把东西paste在myString=""的两个引号之间。
'==========================================
'这时.asp文件已经可以执行,不过生成的是空word文档,啥也没填,没用。
'在myString后面的引号里,找你那些标记,比如姓名一栏你填的是“西瓜”,
'那么现在找到“西瓜”两个字,删除,打"&rs("name")&",
'包括前后两个引号。以此类推,把所有的地方都用库中的纪录搞定。
'========================================
'哦,现在就行了。基本满足要求了。
'注:几个小问题
'1。有时保存你的word文档到web页面时,会生成”你的文件名.files"的文件夹。
'那你用这个.asp文件动态生成word文档时,会提示“XXXX丢失”,不爽!
'解决办法:在你保存的web页面,查看源文件,查找“你的文件名.files“,
'相关的地方都删除掉。一般会有<link...>还有style里的。看着删吧。
'2。这个.asp文件执行是会出现下载提示框,如果选“打开”的话,
'word就会打开生成的这个文档,我发现有时是以“web视图”打开的,
'有时是“页面视图”打开的。很是奇怪,仔细对比了一下,发现:
'只要在你的myString里找<w:WordDocument>,在后面加上<w:View>Print</w:View>,那么就会以“页面视图”打开了。好了!
%>
免责声明:本站相关技术文章信息部分来自网络,目的主要是传播更多信息,如果您认为本站的某些信息侵犯了您的版权,请与我们联系,我们会即时妥善的处理,谢谢合作!