Problem writing HTML content to Word document in ASP.NET
- by saran
I am trying to export the HTML page contents to Word.
My Html display page is:
What is your favourite color?
NA
List the top three school ?
one National
two Devs
three PS
And a button for click event. The button click event will open MS word and paste the page contents in word.
The word page contains the table property of html design page. It occurs only in Word 2003. But in word 2007 the word document contains the text with out table property. How can I remove this table property in word 2003.
I am not able to add the snapshots. Else i will make you clear.
I am designing the web page by aspx. I am exporting the web page content by the following code.
protected void Button1_Click(object sender, EventArgs e)
{
Response.ContentEncoding = System.Text.Encoding.UTF7;
System.Text.StringBuilder SB = new System.Text.StringBuilder();
System.IO.StringWriter SW = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlTW = new System.Web.UI.HtmlTextWriter(SW);
tbl.RenderControl(htmlTW);
string strBody = "<html>" +
"<body>" + "<div><b>" + htmlTW.InnerWriter.ToString() + "</b></div>" +
"</body>" +
"</html>";
Response.AppendHeader("Content-Type", "application/msword");
Response.AppendHeader("Content-disposition", "attachment; filename=" + fileName);
Response.ContentEncoding = System.Text.Encoding.UTF7;
string fileName1 = "C://Temp/Excel" + DateTime.Now.Millisecond.ToString();
BinaryWriter writer = new BinaryWriter(File.Open(fileName1, FileMode.Create));
writer.Write(strBody);
writer.Close();
FileStream fs = new FileStream(fileName1, FileMode.Open, FileAccess.Read);
byte[] renderedBytes;
// Create a byte array of file stream length
renderedBytes = new byte[fs.Length];
//Read block of bytes from stream into the byte array
fs.Read(renderedBytes, 0, System.Convert.ToInt32(fs.Length));
//Close the File Stream
fs.Close();
FileInfo TheFile = new FileInfo(fileName1);
if (TheFile.Exists)
{
File.Delete(fileName1);
}
Response.BinaryWrite(renderedBytes);
Response.Flush();
Response.End();
}