Subek Shakya
Subek Shakya

Reputation: 605

Export data to word in c#

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();
}

Upvotes: 3

Views: 18650

Answers (3)

Micheal kenp
Micheal kenp

Reputation: 11

There is a free solution to export data to word,

http://www.codeproject.com/Articles/151789/Export-Data-to-Excel-Word-PDF-without-Automation-f

Upvotes: 1

Indy9000
Indy9000

Reputation: 8851

Did you try searching the web for this ?

How to automate Microsoft Word to create a new document by using Visual C#

Upvotes: 3

Rajesh Subramanian
Rajesh Subramanian

Reputation: 6490

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

Upvotes: 6

Related Questions