5
votes

I am trying to export the HTML page contents to Word.

My Html display page is:

  1. What is your favourite color?

NA

  1. 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();
}
4
It is very difficult to understand what you are doing or what your problem is. Please try to edit the question, so we can understand what you try to achieve. Thanks!splattne
I am not able to add snapshots. Sorry to say this. Can you tell me that what are all the informations you need more to understandsaran
I try to guess what you are doing: you grab a web page using a C# program (Windows application). Then you "export" it to a Word document? Or are you talking about an ASP.NET application? It is not clear.splattne
Yes splattne. You are right. I am exporting the web page contents to word.saran
Do you use C# in ASP.NET or in a Windows Forms application? Or do you do it "manually" in the web browser?splattne

4 Answers

1
votes

You are writing HTML, claiming it is of content type "application/msword", then hoping for the best..

There are more "correct" ways to achieve your objective.

There are a few projects around for converting (X)HTML to WordML content, of which docx4j-ImportXHTML.NET is one. Disclosure: I maintain that; you can find links to others elsewhere here on StackOverflow.

Alternatively, you can use Word's altChunk mechanism, though note:

  • you have less control over how the import is performed;
  • AltChunk isn't supported by Word 2003 (even with the compatibility pack).
0
votes

You can also try to create an Open XML document that is now recognized by MS office. Here is some more info with code samples: http://msdn.microsoft.com/en-us/library/bb656295.aspx

0
votes

From what I understand, you are trying to create a ms word document on the fly and are having difficulty when the output is viewed in Word 2003 vs. 2007.

In your code above, you are simply spitting out html and forcing it to be a ms word document. I'm surprised it even works.

Instead, you might want to use Office Interop (Microsoft.Office.Interop.Word) or install DocX using nuget. Look at some examples online, search for "C# create word doc".