Click here to Skip to main content
13,046,660 members (67,761 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


14 bookmarked
Posted 20 May 2008

OWC -Office Web Component v11.0 Print Functionality

, 20 May 2008
Rate this:
Please Sign up or sign in to vote.
This article describes, how to achieve print functionality for spreadsheet OWC Component.


During development of an application we came across the situation where we need to print the content of OWC spreadsheet.So after doing some search and research we were successful in getting the solution for above problem. The code given below is self explanatory.The only disadvantage of this solution is that it render and save the content into html file. One can get rid of this mechanism by deleting this file after the print operation is completed.

Important Namespace

using Microsoft.Office.Interop.Owc11;
using System.IO;

Using the code

There are two event :

  • 1.Call 'Save' event to store the XMLData of spreadsheet into hidden variable using javascript
  • 2.Call print event from code behind to print the OWC Spreadsheet content.

    Step1:Javascript-Save Event Function

    function btnSave_Click()
        var spreadsheet = document.getElementById("sp");
        document.getElementById('<%= hdnXML.ClientID %>').value=spreadsheet.XMLData;
        return false;

    Step2 :ASPX

    Note include validateRequest=false in the page directive of aspx page.

        <form id="form1" runat="server">
        <h1>OWC Print Functionality </h1>
        <hr width=1px />
        <object classid="clsid:0002E559-0000-0000-C000-000000000046" id="sp" width="100%"
                        height="80%"><param name="XMLData" value="<?xml version='1.0'?>
            <?mso-application progid='Excel.Sheet'?>
            <Workbook xmlns='urn:schemas-microsoft-com:office:spreadsheet'
             <DocumentProperties xmlns='urn:schemas-microsoft-com:office:office'>
             <ExcelWorkbook xmlns='urn:schemas-microsoft-com:office:excel'>
              <Style ss:ID='Default' 
               <Alignment ss:Vertical='Bottom'/>
             <Worksheet ss:Name='Sheet1'>
              <WorksheetOptions xmlns='urn:schemas-microsoft-com:office:excel'>
              <asp:Button ID="btnPrint" OnClick="btnPrint_Click" runat="server" Text="Print" /> 
              <asp:Button ID="btnsave" OnClientClick="javascript: return btnSave_Click();" runat="server" Text="Save" /> 
        <input type=hidden runat="server" id="hdnXML" />

    Step3:Codebehind-Print Event Function

    protected void btnPrint_Click(object sender, EventArgs e)
                //Export and save the OWC in HTM format.
                SpreadsheetClass spreadSheetClass = new SpreadsheetClass();
                spreadSheetClass.XMLData = hdnXML.Value;
                //Inject Print function into HTM file 
                StreamWriter streamWriter;
                streamWriter = System.IO.File.AppendText(Server.MapPath("OWCXML.htm"));
    ("<html><body onload='window.print()'><table><tr><td></td></tr></table></body></html>");
                //Call Javascript to popup 'OWCXML.htm'
    (this.GetType(), "Key1", "<script>window.showModalDialog('OWCXML.htm','popup'); </script>");
            catch (Exception ex)
                throw ex;


    For more detail on OWC refer article by Gautam Sharma: Office Web Component v11.0 Spreadsheet and AJAX Interoperatibility Part1

  • License

    This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


    About the Author

    santosh poojari
    Technical Lead
    India India
    Whatsup-->Exploring--> MVC/HTML5/Javascript & Virtualization.......!

    You may also be interested in...

    Comments and Discussions

    QuestionCan we open excel with multiple sheets? Pin
    sachin_070010-Nov-10 2:05
    membersachin_070010-Nov-10 2:05 
    Questioncan we open existing excel file using OWC spreadsheet? Pin
    sachin_070014-Oct-10 19:25
    membersachin_070014-Oct-10 19:25 
    Generalimages.. Pin
    kevdelkevdel20-May-08 6:51
    memberkevdelkevdel20-May-08 6:51 
    GeneralRe: images.. Pin
    santosh poojari20-May-08 18:42
    membersantosh poojari20-May-08 18:42 

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

    Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

    Permalink | Advertise | Privacy | Terms of Use | Mobile
    Web01 | 2.8.170713.1 | Last Updated 20 May 2008
    Article Copyright 2008 by santosh poojari
    Everything else Copyright © CodeProject, 1999-2017
    Layout: fixed | fluid