1
votes
 private void ConvertHTMLtoDOCX(string txtcode)
 {
     System.Text.StringBuilder strBody = new System.Text.StringBuilder("");

     strBody.Append("<html " + "xmlns:o='urn:schemas-microsoft-com:office:office' " + "xmlns:w='urn:schemas-microsoft-com:office:word'" + "xmlns='http://www.w3.org/TR/REC-html40'>" + "<head><title>Time</title>");

     //The setting specifies document's view after it is downloaded as Print
     //instead of the default Web Layout
     strBody.Append("<!--[if gte mso 9]>" + "<xml>" + "<w:WordDocument>" + "<w:View>Print</w:View>" + "<w:DoNotOptimizeForBrowser/>" + "</w:WordDocument>" + "</xml>" + "<![endif]-->");


     strBody.Append("<style>" + "<!-- /* Style Definitions */" + "@page Section1" + "   {size:8.5in 11.0in; " + "   margin:1.0in 1.25in 1.0in 1.25in ; " + "   mso-header-margin:.5in; " + "   mso-footer-margin:.5in; mso-paper-source:0;}" + " div.Section1" + "   {page:Section1;}" + "-->" + "</style></head>");

     strBody.Append("<body lang=EN-US style='tab-interval:.5in'>" + "<div class=Section1>" + Html_editor.Content + "</div></body></html>");

     //Force this content to be downloaded 
     //as a Word document with the name of your choice


     string FullFilePath = @"C:\Users\ravikant\Desktop\AR GitHub\07-05-2014\FinalTestARGithub\LetterTemplate\"+ txtcode+ ".docx"; 

     FileInfo file = new FileInfo(FullFilePath);
     if (file.Exists)
     {
        ClientScript.RegisterStartupScript(this.GetType(), "disExp", "<script>alert('File Already Exists');</script>");
     }
     else
     {
         Response.AppendHeader("Content-Type", "application/vnd.openxmlformats-officedocument.wordprocessingml.document");
         Response.AppendHeader("Content-disposition", "inline; filename="+txtcode+".docx");
         Response.Write(strBody);
     }       
 }

Here is the code using the CONTENT-TYPE for .DOCX "application/vnd.openxmlformats-officedocument.wordprocessingml.document", the Content is corrupt while opening the file.

1
Check the file size of the file. Does it look like there is anything actualy saved into the file? Also I think your file.Exists check is mistaken. That will check if the file exists on the server, where as you are saving to the client. On your dev machine that is likley to be the same, but once you publish to a remote server you'll have issues. - Jon P
the file size is 200kb its fine, it shows that the problem in the content. Don't know why is this occurss, when i make the.doc or .xlsx this is make easy. No error occurs while compile the Code also, plz look into this matter. - Ravi Kant Singh
As a first step, I'd remove the editor content from what you are saving. Put a basic <p>Hello World</p> in its place. If that works you know the issue is with the content from the editor. Check that content for html, xml, doctype, head and body tags. If it doens't work, there is something wrong with your saving procedure. - Jon P
The file.exists are working fine, no issue from there. Acutely i also create the file in different format, the file is created easily but for .DOCX i dont know. - Ravi Kant Singh
Another hack that might help, I've done this with native word .docx, but not .docx generated in this manner, so it may or may not work. Make a copy of the saved file, change its extention from .docx to .zip. Try and open that. We are trying to find a file document.xml, which is normally in the "word" folder. Open that in a text editor an see if anything is jumping out as wrong or try putting it through an XML validator. VisualStudio should be good enough to show any malformating. - Jon P

1 Answers

1
votes

Try this to find out what is going on with the file.

I've done this with native word .docx, but not .docx generated in this manner, so it may or may not work.

  1. Make a copy of the saved file, change its extention from .docx to .zip.
  2. Try and open that. We are trying to find a file document.xml, which is normally in the "word" folder.
  3. Open that in a text editor an see if anything is jumping out as wrong or try putting it through an XML validator. VisualStudio should be good enough to show any malformating.

Online XML Validator that might help: http://www.xmlvalidation.com/

The following lines are also suspect:

strBody.Append("<!--[if gte mso 9]>" + "<xml>" + "<w:WordDocument>" + "<w:View>Print</w:View>" + "<w:DoNotOptimizeForBrowser/>" + "</w:WordDocument>" + "</xml>" + "<![endif]-->");

As I'm unsure how word will handle IE conditional comments. Comment out or remove this line and see what happens.

strBody.Append("<style>" + "<!-- /* Style Definitions */" + "@page Section1" + "   {size:8.5in 11.0in; " + "   margin:1.0in 1.25in 1.0in 1.25in ; " + "   mso-header-margin:.5in; " + "   mso-footer-margin:.5in; mso-paper-source:0;}" + " div.Section1" + "   {page:Section1;}" + "-->" + "</style></head>");

Due to the nested comments. <!-- /* */-->. Perhaps try changing it to: strBody.Append("</head>"); and see if that works.