日期:2013-05-06  浏览次数:20869 次

在网上,利用Asp生成Word文档的方法实在不少,最常见到的方法就是利用Word生成.dot的文档,再用Asp向.dot文档中传递宏,这种方法虽好,但实现起来非常复杂,而且不能循环生成表格。

在实际应用中,我总结出来一种新的生成Word文件的方法,生成的Word文件有页眉、页角,现拿出一例与大家探讨。



实现:

我的解决办法,用Word XP(其他版本还没有试过)制作表格,将做好的Word表格转换为HTML文档,在用ASP控制转换后HTML文档。

页眉、页角的解决办法,在转换带Word页眉、页角的DOC文档后会在转换的HTML文件附加一个文件夹,内有页眉、页角文件及图片,只要在HTML文件的相应位置改变其路径为服务器路径即可。用户打开并ASP生成的Word文件后,Word会自动提示将这些文件保存。

看下面的Word转换后的HTML文,我用ASP加以简单控制:

<% Response.ContentType = "application/msword" %>
<!-- #include file="inc/Conn_Open.asp"-->
<!-- #include file="inc/Transition.asp"-->
<!-- #include file="inc/Check_Character.asp"-->
<!-- #include file="inc/ErrorMsg.asp"-->
<!-- #include file="inc/Check_Privilege.asp"-->
<!-- #include file="inc/Check_Logout.asp"-->
<!-- #include file="inc/System_Log_Action.asp"-->
<%response.buffer=false
response.expires=0
call Check_Logout()
call check_state()
call Privilege(6)
call Log_Action("访问","销售编号打印","打印临时销售编号"&request("Temp_Id"),1)%>
%>
<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 10">
<meta name=Originator content="Microsoft Word 10">
<link rel=File-List href="create_doc/filelist.xml">
<link rel=Edit-Time-Data href="create_doc/editdata.mso">
<link rel=OLE-Object-Data href="create_doc/oledata.mso">
<!--[if gte mso 9]><xml>
<o:DocumentProperties>
<o:Author>Administrator</o:Author>
<o:LastAuthor>Administrator</o:LastAuthor>
<o:Revision>34</o:Revision>
<o:TotalTime>26</o:TotalTime>
<o:LastPrinted>2002-11-06T05:54:00Z</o:LastPrinted>
<o:Created>2002-11-06T04:56:00Z</o:Created>
<o:LastSaved>2002-11-06T05:55:00Z</o:LastSaved>
<o:Pages>1</o:Pages>
<o:Words>98</o:Words>
<o:Characters>562</o:Characters>
<o:Company>shine</o:Company>
<o:Lines>4</o:Lines>
<o:Paragraphs>1</o:Paragraphs>
<o:CharactersWithSpaces>659</o:CharactersWithSpaces>
<o:Version>10.2625</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:Compatibility>
<w:UseFELayout/>
</w:Compatibility>
<w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel>
</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.MsoNor