2011-03-19 40 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

相關問題