读书人

关于用asp操作word表格的有关问题

发布时间: 2012-03-24 14:00:46 作者: rapoo

关于用asp操作word表格的问题?
<%Dim fso, MyFile
Set fso = CreateObject( "Scripting.FileSystemObject ")
sFileName = "temp.doc "
Set MyFile = fso.CreateTextFile(Server.MapPath( ". ") & "\word\ "&sFileName, True)
myString= " <html xmlns:v=““urn:schemas-microsoft-com:vml”“ 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 11”“> <meta name=Originator content=““Microsoft Word 11”“> <link rel=File-List href=““Doc1.files/filelist.xml”“> <title> 石油化工学院教师业务档案 </title> <!--[if gte mso 9]> <xml> <o:DocumentProperties> <o:Author> lyz </o:Author> <o:LastAuthor> lyz </o:LastAuthor> <o:Revision> 2 </o:Revision> <o:TotalTime> 48 </o:TotalTime> <o:Created> 2004-03-27T04:56:00Z </o:Created> <o:LastSaved> 2004-03-27T04:56:00Z </o:LastSaved> <o:Pages> 1 </o:Pages> <o:Words> 92 </o:Words> <o:Characters> 527 </o:Characters> <o:Company> 602 </o:Company> <o:Lines> 4 </o:Lines> <o:Paragraphs> 1 </o:Paragraphs> <o:CharactersWithSpaces> 618 </o:CharactersWithSpaces> <o:Version> 11.5606 </o:Version> </o:DocumentProperties> </xml> <![endif]--> <!--[if gte mso 9]> <xml> <w:WordDocument> <w:View> Print </w:View> <w:SpellingState> Clean </w:SpellingState> <w:GrammarState> Clean </w:GrammarState> <w:PunctuationKerning/> <w:DrawingGridVerticalSpacing> 7.8 磅 </w:DrawingGridVerticalSpacing> <w:DisplayHorizontalDrawingGridEvery> 0 </w:DisplayHorizontalDrawingGridEvery> <w:DisplayVerticalDrawingGridEvery> 2 </w:DisplayVerticalDrawingGridEvery> <w:ValidateAgainstSchemas/> <w:SaveIfXMLInvalid> false </w:SaveIfXMLInvalid> <w:IgnoreMixedContent> false </w:IgnoreMixedContent> <w:AlwaysShowPlaceholderText> false </w:AlwaysShowPlaceholderText> <w:Compatibility> <w:SpaceForUL/> <w:BalanceSingleByteDoubleByteWidth/> <w:DoNotLeaveBackslashAlone/> <w:ULTrailSpace/> <w:DoNotExpandShiftReturn/> <w:AdjustLineHeightInTable/> <w:BreakWrappedTables/> <w:SnapToGridInCell/> <w:WrapTextWithPunct/> <w:UseAsianBreakRules/> <w:DontGrowAutofit/> <w:UseFELayout/> </w:Compatibility> <w:BrowserLevel> MicrosoftInternetExplorer4 </w:BrowserLevel> </w:WordDocument> </xml> <![endif]--> <!--[if gte mso 9]> <xml> <w:LatentStyles DefLockedState=““false”“ LatentStyleCount=““156”“> </w:LatentStyles> </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;} span.SpellE {mso-style-name:”“““; mso-spl-e:yes;} /* 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 70.9pt 72.0pt 70.9pt; mso-header-margin:42.55pt; mso-footer-margin:49.6pt; mso-paper-source:0;




[解决办法]
我在用ASP为某单位制作网页时遇到这样一个问题,单位以前的MIS系统中将一些Word文件以字节流的形式保存在数据库中,现在用户要求我用ASP将这些Word文件数据从数据库中取出并在网页中显示出来。开始我自然地想到在服务器上创建临时文件、然后在网页中增加一个指向这个临时文件的链接,但这个方法将大大增加服务器的负担不说,而且在服务上如何保证特定客户端所使用的临时文件不被其它客户端使用的文件覆盖,如何在文件传送给用户后将文件删除,这些问题在实际都难很好解决。那么有没有更好的办法呢?

---- 为此我仔细查看了一下ASP的参考书,发现Response对象有一个叫contenttype的属性,它定义服务器发送给客户端内容的MIME类型。MIME全称Multipurpose Internet Mail Extensions,即多功能Internet邮件扩展。我们知道,在网页编程中我们有时将超链接指向一个Word或Excel文件,当用户点击这个链接时浏览器会自动调用对应方法将这个文件打开。之所以能做到这点就是因为用户机器上安装office后会在浏览器中注册对应的MIME资源类型。比如说word文件的MIME类型是Application/msword(前者是MIME类型,后者是MIME子类),Excel文件的MIME资源类型是Application/msexcel。事实上,凡是浏览器能处理的所有资源都有对应的MIME资源类型,比如说html文件的MIME类型是Text/html,JPG文件的MIME类型是Image/JPG。在与服务器的交互中,浏览器就是根据所接受数据的MIME类型来判断要进行什么样的处理,对html、JPG等文件浏览器直接将其打开,对Word、Excel等浏览器自身不能打开的文件则调用相应方法打开。对没有标记MIME类型的文件,浏览器则根据其扩展名和文件内容猜测其类型。如果浏览器无法猜出,则将它作为application/octet-stream。要了解各种文件的MIME类型,请在win98 我的电脑-> 查看-> 文件夹选项-> 文件类型 中查看。

---- 于是我灵机一动,想到在ASP中可以先将WORD数据以字节流方式取出,接着将其conntenttype属性标记为Application/msword,再将它发送给客户机,客户机收到这个资源后,根据其MIME类型,会自动调用客户机上的Word(当然,前提是客户机上装了Word,否则会将其作为一个不能识别的资源,提示用户保存起来,而不是打开它)将它打开。经试验效果很好,方法简单且速度很快,而且在IE5中浏览器使用内嵌方式(类似于OLE方式)打开,效果更佳。以下是程序内容。

---- 假设表名tab_word,表中有两个字段,一个是整型,名id,用作Word数据的唯一标识,另一个Blob型,名worddata,里面存放Word数据。现在要在页面上显示id等于1的Word文件内容, ASP程序如下:

< %
' conn - 已创建的数据库连接
' rs -- 结果集
rs = conn.execute( "select
worddata from tab_word where id = 1 ")
response.contenttype = "Application/msword "
response.writebinary(rs( "worddata "))
'注意将结果集中的数据直接用writebinary发送出去,不要用变量
'接收这个数据,否则系统会报错
% >

---- 用类似的方法,还可以处理Excel、Bmp等许多类型的数据。此程序在以下系统中测试通过:服务器:NT + IIS + SqlServer客户端:Win98 + IE5 或 Netscape4.x


5
又看了这个,试了没成功,共享:

<HTML>
<HEAD>
<meta HTTP-EQUIV= "Content-Type " content= "text/html; charset=gb_2312-80 ">
<link rel= "stylesheet " type= "text/css " href= "edit.css ">
<title> 保存为Word文档 </title>
</HEAD>
<script language= "javascript ">
window.returnValue=0
</script>
<BODY>
<font color= "#000080 "> <strong> <div id= "strHint " align= "center "> 数据处理中,请等待... <br/>
Data processing, please wait... </div> </strong> </font> <font color= "#0080C0 ">


<script language= "vbscript ">

On Error Resume Next


6
document.SaveAs "FileName.txt ", 7 'wdFormatUnicodeText


wdFormatDocument = 0
wdFormatTemplate = 1
wdFormatText = 2
wdFormatTextLineBreaks = 3
wdFormatDOSText = 4
wdFormatDOSTextBreakLines = 5
wdFormatRTF = 6
wdFormatEncodedText = 7
wdFormatUnicodeText = 7
wdFormatHTML = 8


Dim wApp

Set wApp = CreateObject( "Word.Application ")
If Err.number > 0 Then
Alert "没法保存为Word文件,请正确安装Word97 "


else
wApp.visible = True
wApp.Documents.add

wApp.Selection.TypeParagraph
wApp.Selection.Font.Bold = True
wApp.Selection.TypeText "九八Joy ASP维护工作意见 "

wApp.Selection.ParagraphFormat.Alignment = 1
rem 居 中

wApp.Selection.TypeParagraph
wApp.Selection.Font.Bold = false

wApp.Selection.TypeText "-- 飞鸟 "

wApp.Selection.TypeParagraph

wApp.Selection.ParagraphFormat.LeftIndent = wApp.CentimetersToPoints(0)
wApp.Selection.ParagraphFormat.FirstLineIndent = wApp.CentimetersToPoints(0.72/2*2)

wApp.Selection.TypeParagraph
wApp.Selection.Font.Bold = false
wApp.Selection.ParagraphFormat.Alignment = 0


wApp.Selection.TypeText "认真落实CHINA ASP为网友解难题,办实事精神,强化运行管理,优化网络;坚持以网管为龙头,以提高接通率为重点,做好运维工作,保证全网运行安全、高效、畅通。认真落实局内六十项奋斗目标,围绕效益工程、形象工程、福利工程,开动脑筋、解放思想,以改革为动力,推动维护、服务、管理工作更上台阶。 "
wApp.Selection.TypeParagraph

rem 居 右
wApp.Selection.TypeParagraph
wApp.Selection.Font.Bold = false
wApp.Selection.TypeText "一九九九年五月 "
wApp.Selection.TypeParagraph

end if
</script>
<center> <br/> <strong> 数据处理完毕! </strong> </center>

<p>
</p> <div align= 'center '> <center> <input type=button class=buttonface name= 'cmdOK ' onclick= "window.returnValue=0;window.close(); " value= " 确 定 " > </center> </div>

</BODY>
</HTML>

读书人网 >ASP

热点推荐