当前分类:IT互联网

asp生成word文档

 今天在处理一个在线申请功能时候,需要生成word文档。在网上找了些资料学习,终于成功了。下面是个简单的例子。其中程序包括index.htm,createdoc.asp。在之前你需要做一个word模板,并另存为web页。然后则是处理web页的代码:

1、用“记事本”的方式打开你的web页,并复制所有的代码。

2、新建一个word  将上一步复制好的代码粘贴进来,然后按住ctrl+F,点“替换”标签,点“高级”,查找内容里填“特殊字符”的“段落标记”,替换为填“特殊字符”的“不间断空格”,全部替换即可。然后复制全部的字符(ctrl+a,ctrl+c即可)。

3、使用dreamweaver打开createdoc.asp,把东西复制在myString=""的两个引号之间。选择上刚你复制过来的所有字符,ctrl+F,查找范围选择为“所选文字”,查找内容填一个双引号",替换为填两个双引号"",全部替换即可。(这样是因为需要输出双引号。在asp的输出中两个双引号等于输出一个双引号,起到了转义的效果。)

 说明:网上查询的资料是在word中替换好了双引号,可我好几次都出现了错误未能成功,所以干脆到

 dreamweaver中去替换)

:打开生成的这个word文档,如果发现是以“web视图”的方式打开的,只要在你的myString里找<w:WordDocument>,在后面加上<w:View>Print</w:View>,那么就会以“页面视图”打开了。

word文档模板

asp生成word文档(一)

index.htm:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

 

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=gb2312"  />

<title>无标题文档</title>

</head>

 

<body>

<form id="form1" name="form1" method="post" action="createdoc.asp">

<table width="100%" border="0" cellspacing="0" cellpadding="0">

 <tr>

   <td width="21%">姓名</td>

   <td width="79%"><input type="text" name="myname"  /></td>

 </tr>

 <tr>

   <td>个人资料</td>

   <td><input type="text" name="mycontent"  /></td>

 </tr>

</table>

<input name="submit1" type="submit" value="提交" />

</form>

</body>

</html>

 

createdoc.asp:

<%

myname=request.Form("myname")

mycontent=request.Form("mycontent")

dim fso,myfile

set fso=Server.CreateObject("Scripting.FileSystemObject")

sfilename=myname&".doc"

set  myfile=fso.CreateTextFile(Server.MapPath(".")&"\word"&sfilename,True)

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 10""> <meta name=Originator  content=""Microsoft Word 10""> <link rel=File-List  href=""temp.files/filelist.xml""> <title>姓名</title> <!--[if  gte mso 9]><xml> <o:DocumentProperties>  <o:Author>andy</o:Author>  <o:LastAuthor>andy</o:LastAuthor>  <o:Revision>2</o:Revision>  <o:TotalTime>3</o:TotalTime>  <o:Created>2009-02-08T04:31:00Z</o:Created>  <o:LastSaved>2009-02-08T04:31:00Z</o:LastSaved>  <o:Pages>1</o:Pages>  <o:Words>4</o:Words>  <o:Characters>24</o:Characters>  <o:Company>balansell</o:Company>  <o:Lines>1</o:Lines>  <o:Paragraphs>1</o:Paragraphs>  <o:CharactersWithSpaces>27</o:CharactersWithSpaces>  <o:Version>10.6839</o:Version> </o:DocumentProperties>  </xml><![endif]--><!--[if gte mso 9]><xml> <w:WordDocument>  <w:View>Print</w:View>  <w:GrammarState>Clean</w:GrammarState>  <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:BreakWrappedTables/>   <w:SnapToGridInCell/>   <w:WrapTextWithPunct/>   <w:UseAsianBreakRules/>   <w:UseFELayout/>  </w:Compatibility>  <w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel> </w:WordDocument> </xml><![endif]--> <style>  <!--  @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;}  p.MsoNormal, li.MsoNormal, div.MsoNormal   {mso-style-parent:"""";  margin:0;  margin-bottom:0;   text-align:justify;  text-justify:inter-ideograph;   mso-pagination:none;  font-size:14px;   mso-bidi-font-size:16px;  font-family:""Times New Roman"";   mso-fareast-font-family:宋体;  mso-font-kerning:1px;}  @page  {mso-page-border-surround-header:no;   mso-page-border-surround-footer:no;} @page Section1  {size:794px  1123px;  margin:96px 120px 96px 120px;   mso-header-margin:57px;  mso-footer-margin:66px;   mso-paper-source:0;  layout-grid:21px;} div.Section1   {page:Section1;} --> </style> <!--[if gte mso 10]>  <style>  table.MsoNormalTable  {mso-style-name:普通表格;   mso-tstyle-rowband-size:0;  mso-tstyle-colband-size:0;   mso-style-noshow:yes;  mso-style-parent:"""";   mso-padding-alt:0 7px 0 7px;  mso-para-margin:0;   mso-para-margin-bottom:0;  mso-pagination:widow-orphan;   font-size:13px;  font-family:""Times New Roman"";}  table.MsoTableGrid  {mso-style-name:网格型;  mso-tstyle-rowband-size:0;   mso-tstyle-colband-size:0;  border:solid windowtext 1px;   mso-border-alt:solid windowtext 1px;  mso-padding-alt:0 7px 0  7px;  mso-border-insideh:1px solid windowtext;   mso-border-insidev:1px solid windowtext;  mso-para-margin:0;   mso-para-margin-bottom:0;  text-align:justify;   text-justify:inter-ideograph;  mso-pagination:none;   font-size:13px;  font-family:""Times New Roman"";} </style>  <![endif]--> </head> <body  style='tab-interval:28px;text-justify-trim:punctuation'> <div style='layout-grid:21px'> <table  border=1 cellspacing=0 cellpadding=0 style='border-collapse:collapse;border:none;mso-border-alt:solid windowtext  1px; mso-yfti-tbllook:480;mso-padding-alt:0 7px 0  7px;mso-border-insideh: 1px solid windowtext;mso-border-insidev:1px  solid windowtext'> <tr style='mso-yfti-irow:0'>  <td width=103 valign=top style='width:103px;border:solid windowtext  1px;  mso-border-alt:solid windowtext 1px;padding:0 7px 0  7px'>  <p><span  style='font-family:宋体;mso-ascii-font-family:""Times New Roman"";  mso-hansi-font-family:""Times New  Roman""'>姓名</span></p>  </td>  <td  width=465 valign=top style='width:465px;border:solid windowtext  1px;  border-left:none;mso-border-left-alt:solid windowtext  1px;mso-border-alt:  solid windowtext 1px;padding:0 7px 0  7px'>  <p><span>$name$</span></p>  </td> </tr> <tr  style='mso-yfti-irow:1;mso-yfti-lastrow:yes'>  <td width=103  valign=top style='width:103px;border:solid windowtext 1px;  border-top:none;mso-border-top-alt:solid windowtext 1px;mso-border-alt:solid  windowtext 1px;  padding:0 7px 0 7px'>  <p><span style='font-family:宋体;mso-ascii-font-family:""Times  New Roman"";  mso-hansi-font-family:""Times New  Roman""'>个人资料</span></p>  </td>  <td width=465 valign=top  style='width:465px;border-top:none;border-left:  none;border-bottom:solid windowtext 1px;border-right:solid windowtext  1px;  mso-border-top-alt:solid windowtext  1px;mso-border-left-alt:solid windowtext 1px;  mso-border-alt:solid  windowtext 1px;padding:0 7px 0 7px'>  <p><span>$content$</span></p>  </td> </tr> </table> <p><span><o:p>&nbsp;</o:p></span></p> </div> </body> </html>"   'word文档模板另存为web页后经过处理了的所有字符

mystring=replace(mystring,"$name$",myname) '$name$为word文档模板中需要输入的内容之一

mystring=replace(mystring,"$content$",mycontent)'$content$为word文档模板中需要输入的内容之一

myfile.WriteLine(mystring)

myfile.close

set myfile=nothing

set fso=nothing

%>


相关文章: