2011-03-19 63 views
0

我正在写一个ASP.NET应用程序在C#中,我试图写一个表格到一个Word文档。ASP.NET:写一个表格(和复选框)到Word文档

我目前使用下面的代码写入到一个Word文档:

 string strContent = "This content goes to the word document"; 
     string attach = "attachment; filename=wordtest.doc"; 
     HttpContext.Current.Response.Clear(); 
     HttpContext.Current.Response.Charset = ""; 

     HttpContext.Current.Response.ContentType = "application/msword"; 
     HttpContext.Current.Response.AddHeader("content-disposition", attach); 
     HttpContext.Current.Response.Write(strContent); 
     HttpContext.Current.Response.End(); 
     HttpContext.Current.Response.Flush(); 

现在,我的问题是,如果有人知道我怎么能写一个表到Word文档?

而第二个问题是,如果有人知道如何写复选框到Word文档(如您从网站复制并粘贴到字)

提前感谢!

Killerwes

+0

看我的帖子 – AEMLoviji 2011-03-19 09:08:39

回答

1

Word可以识别HTML。所以你可以用Response.Write()来简单地写任何HTML到MS WORD。方法。这里是代码示例。

string strBody = "<html>" + 

      "<body>" + 

       "<div>Your name is: <b>" + txtName.Text + "</b></div>" + 

       "<table width="100%" style="background-color:#cfcfcf;"><tr><td>1st Cell body data</td><td>2nd cell body data</td></tr></table>" + 

       "Ms Word document generated successfully." + 

      "</body>" + 

      "</html>"; 

     string fileName = "MsWordSample.doc"; 

     // You can add whatever you want to add as the HTML and it will be generated as Ms Word docs 

     Response.AppendHeader("Content-Type", "application/msword"); 

     Response.AppendHeader ("Content-disposition", "attachment; filename="+ fileName); 

     Response.Write(strBody); 
+0

谢谢你,不知道你可以使用HTML标签!你碰巧知道如何禁用复选框?我尝试禁用=“禁用”,但没有工作 – Killerwes 2011-03-20 09:39:26

+0

我从来没有尝试禁用任何控制并将其保存在MS WORD上。但你可以谷歌它。但我可以告诉你,你可以保存html到MS WORD和MS Excel。 – AEMLoviji 2011-03-20 17:54:23

相关问题