Export data to word in c#

c#, ms-word, winforms

Solution

1. Interop API

It is available in Namespace `Microsoft.Office.Interop.Word`.

You can use Word Interop COM API to do that using following code,

// Open a doc file.
    Application application = new Application();
    Document document = application.Documents.Open("C:\\word.doc");

    // Loop through all words in the document.
    int count = document.Words.Count;
    for (int i = 1; i <= count; i++)
    {
        // Write the word.
        string text = document.Words[i].Text;
        Console.WriteLine("Word {0} = {1}", i, text);
    }
    // Close word.
    application.Quit();

Only Drawback is you must have office installed to use this feature.

2. OpenXML you can use openxml to build word documents, try the following link,

http://msdn.microsoft.com/en-us/library/bb264572(v=office.12).aspx

Problem

Below is the code that creates a pdf to write a file..Every time i call the below code it creates a pdf file to write into..My question is,is there a same method for exporting to word or for simplicity just creates a blank doc file so that i can export data into it.. ``` public void showPDf() { iTextSharp.text.Document doc = new iTextSharp.text.Document( iTextSharp.text.PageSize.A4); string combined = Path.Combine(txtPath.Text,".pdf"); PdfWriter pw = PdfWriter.GetInstance(doc, new FileStream(combined, FileMode.Create)); doc.Open(); } ```

Original source