ASP.NET: Write a table (and checkboxes) to a word document
I am writing an ASP.NET Application in c# and I'm trying to write a table to a word document.
I use currently the following code to write to a word doc:
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();
开发者_Python百科Now, my question is if anyone knows how I can write a table to a word document?
And second question is if anyone knows how to write checkboxes to a word doc (like you copy it from a website and paste it in word)
Thanks in advance!
Killerwes
Word can recognise HTML. So you can simply write any HTML to MS WORD with Response.Write(); Method. Here is code sample.
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);
精彩评论