none
Convert to word format (*.doc) RRS feed

  • Question

  • Hi, I have built my own word processsor application based on web (similar like google docs) and it uses a different xml format to hold and format datas

    I want to convert my own document files to *.doc. How I can do this? I guess I need to convert my xml to ms word's xml?

    Thursday, May 12, 2011 8:49 PM

Answers

  • Hi Cemiltokatli,

    Sorry for the late reply.

    I have a suggestion for you to take, you can use Open XML SDK and Word 2007 or Word 2010 to make it. The content can be extract from the web page and then use Open XML SDK to generate the docx file. For more details, you can refer to the following code snippet:

     

    using System;
    using DocumentFormat.OpenXml.Packaging;
    using System.IO;
    using System.Text;
    using DocumentFormat.OpenXml;
    
    namespace OpenXmlCreateWordDocx
    {
     public partial class WebForm1 : System.Web.UI.Page
     {
      protected void Page_Load(object sender, EventArgs e)
      {
       
      }
    
      public static string txt = "";
      protected void btnGenerateDocx_Click(object sender, EventArgs e)
      {
       string path = Server.MapPath(".");
       txt = this.TextBox1.Text;				// here you can get the content from the web page
       try
       {
        CreateNewWordDocument(path + "\\test.docx");
        Response.Write("<script>alert('创建文档成功');</script>");
       }
       catch (Exception ex)
       {
        Response.Write("<script>alert('创建文档失败');</script>");
       }
      }
    
      // How to create a new package as a Word document.
      public static void CreateNewWordDocument(string document)
      {
       using (WordprocessingDocument wordDoc = WordprocessingDocument.Create(document, WordprocessingDocumentType.Document))
       {
        // Set the content of the document so that Word can open it.
        MainDocumentPart mainPart = wordDoc.AddMainDocumentPart();
    
        SetMainDocumentContent(mainPart);
       }
      }
    
      // Set the content of MainDocumentPart.
      public static void SetMainDocumentContent(MainDocumentPart part)
      {
       StringBuilder docXml = new StringBuilder();
       docXml.Append("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>");
        docXml.Append("<w:document xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\">");
       docXml.Append("<w:body><w:p><w:r><w:t>");
       docXml.Append(txt);
       docXml.Append("</w:t></w:r></w:p></w:body>");
       docXml.Append("</w:document>");
       using (Stream stream = part.GetStream())
       {
        byte[] buf = (new UTF8Encoding()).GetBytes(docXml.ToString());
        stream.Write(buf, 0, buf.Length);
       }
      }
     }
    }
    

     

    After generating the word document, you can use Word 2007 or 2010 to save it as the Word 2003 format.

    Hope this suggestion can give you the hint.

    Best Regards,


    Bruce Song [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    • Marked as answer by Bruce Song Friday, May 27, 2011 11:14 AM
    Wednesday, May 18, 2011 3:04 AM