Sunday, September 5, 2010

Export Silverlight charts to MS word

Writing MS word file with Silverlight needs out of browser functionality but you can avoid this by creating a word file on server and then send this created file back to client using a HTTPhandler. This sample word export application is created using the second approach.
I have created an object oriented design for this component as I will soon extend this to a complete library for exporting Silverlight UI (chart) to DOC, XLS,PDF and other image formats.
Using the code:
  1. Place “MSWordExportHandler.ashx” and “MSWordExportHandler.cs” clientweb folder of the web application hosting your Silverlight application.
  2. Add reference to “Microsoft.Office.Interop.Excel.dll” and “Microsoft.Office.Interop.Word.dll” in you server application (web application) hosting your Silverlight application.
  3. Add reference of the component to your Silverlight project.
  4. Create an object of Export Façade class in your Silverlight page like the following
  5. ExportFacade Exporter = new ExportFacade("DOC (*.Doc)|*.doc");
  6. Export your UIelement i.e Silverlight chart by calling the Exporter.Export(UIElement Chart);
  7. ExportFacade class expose two event that you optionally can capture “ExportStarted” and “ExportCompleted”
Complete snippet of the click event of export button  

 private void Button_Click(object sender, RoutedEventArgs e)
        {
            ExportFacade Exporter = new ExportFacade("DOC (*.Doc)|*.doc");
            Exporter.ExportStarted += new ExportFacade.ExportStartedDelegate(Exporter_ExportStarted);
            Exporter.ExportCompleted += new ExportFacade.ExportCompletedDelegate(Exporter_ExportCompleted);
            Exporter.Export(crtChart);
        }


Sample Code

7 comments:

  1. Hi,
    Your Topic seems hot, but I can not download sample code from given URL: http://www.abubakardar.com/blogcodes/SilverlightExport.rar

    ReplyDelete
  2. I am able to get the code. How can I get it.

    ReplyDelete
  3. I am not able to get the code.. sorry about the above post

    ReplyDelete
  4. Prakash,
    Please send me a mail for source at abubkr.dar@gmail.com
    actually my domain is expired , so you are unable to download it from there :)


    thanks,
    Abubakar

    ReplyDelete
  5. There were lots of requests for source in my inbox. So, I have uploaded source for this post on sky drive as well above link for source is also updated.
    https://skydrive.live.com/redir.aspx?cid=8e18b61e7a140d6f&resid=8E18B61E7A140D6F!106

    ReplyDelete
  6. Code is not there on https://skydrive.live.com/redir.aspx?cid=8e18b61e7a140d6f&resid=8E18B61E7A140D6F!106. Please upload again.

    Thanks,
    Sachin

    ReplyDelete
    Replies
    1. Microsoft.Office.Interop.Excel.dll cannot be added as a reference in silverlight application. Please suggest, what to do.

      Delete