Saturday, November 23, 2013

creating word documents using open xml with

Now in this article i will explain how to use the Office Open XML C# (alternate solution to the Interop object. 

The Open XML provides you set of .NET API. It helps helps you manipulate and creating the documents in the Open XML Formats
in both the environments (i.e client and server both) and that is without help of MS office applications.

Before using Open XML Format, you have to add the below namespaces in your C# class. Add references to the Packaging and Spreadsheet API components with the following code.
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
Here is the CreateOpenXmlDocument function where you need to pass the filepath and it will created that doc file on specified positionl
public void CreateOpenXmlDocument(string filepath)
 using (WordprocessingDocument wordDocument = WordprocessingDocument.Create(filepath, WordprocessingDocumentType.Document))
    // Add a main document part.
    MainDocumentPart mainPart = wordDocument.AddMainDocumentPart();

    mainPart.Document = new Document();
    Body body = mainPart.Document.AppendChild(new Body());
    Paragraph para = body.AppendChild(new Paragraph());
    Run run = para.AppendChild(new Run());
    run.AppendChild(new Text("Create text sample text, coding stuffs daily..."));

Here now i am integrating that function into my page_load event.

protected void Page_Load(object sender, EventArgs e)

      string FilePath =  @"C:\\Temp";

      FileInfo myDoc = new FileInfo(FilePath);
      Response.ContentType = "Application/msword";
      Response.AddHeader("content-disposition", "attachment;filename=" + myDoc.Name);
      Response.AddHeader("Content-Length", myDoc.Length.ToString());
      Response.ContentType = "application/octet-stream";

This is just an sample doc creation , you may even format the document with adding images, graphs, style to para and may more. Just refer this Microsoft article on how to do this. 

feel free to raise your suggestion/comments on this. Thanks you and happy coding....


Tuesday, November 12, 2013

Microsoft.Office.Interop.Word how to prevent word file opening

In my previous post i explained, how to Convert the dataset to xml string using  C# and
Now in this article i will explain how to prevent the word file opening, i have bunch of word document and i want to read/search text value out of it.  i tried that using object. 

Here is the sample code which prevent the word to opening the main window.
private void PreventOpenWord(string Filepath)
 Application word = new Application();
 FileInfo fileInfo = (FileInfo)file;

object save = false;
object confirmConversion = false;
object visible = false;
object skipEncodingDialog = true;
object readOnly = true;
object filename = fileInfo.FullName;

word.Visible = false;

Document srcDoc = word.Documents.Open(ref Filepath, ref confirmConversion, ref readOnly, ref missing,
    ref missing, ref missing, ref missing, ref missing,
    ref missing, ref missing, ref missing, ref visible,
    ref missing, ref missing, ref skipEncodingDialog, ref missing);

foreach (Microsoft.Office.Interop.Word.Range docRange in doc.Words)
if (docRange.Text.Trim().Equals(textToFind,
IsWordFound = true;
catch (Exception ex)
 Response.Write("Error while reading file : " + ex.Message);

Hope this will helps you.. Happy  coding..