Click here to Skip to main content
6,595,854 members and growing! (17,412 online)
Email Password   helpLost your password?
Web Development » ASP.NET » Printing License: The Code Project Open License (CPOL)

OWC -Office Web Component v11.0 Print Functionality

By santosh poojari

This article describes, how to achieve print functionality for spreadsheet OWC Component.
C# (C# 1.0, C# 2.0, C# 3.0)
Posted:20 May 2008
Views:8,743
Bookmarked:9 times
Unedited contribution
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
3 votes for this article.
Popularity: 0.86 Rating: 1.80 out of 5
2 votes, 66.7%
1

2

3

4
1 vote, 33.3%
5

Introduction

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.

    
    <body>
        <form id="form1" runat="server">
        <div>
        <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'
             xmlns:o='urn:schemas-microsoft-com:office:office'
             xmlns:x='urn:schemas-microsoft-com:office:excel'
             xmlns:ss='urn:schemas-microsoft-com:office:spreadsheet'
             xmlns:html='http://www.w3.org/TR/REC-html40'>
             <DocumentProperties xmlns='urn:schemas-microsoft-com:office:office'>
              <Author></Author>
              <LastAuthor></LastAuthor>
              <Created></Created>
              <Company></Company>
              <Version></Version>
             </DocumentProperties>
             <ExcelWorkbook xmlns='urn:schemas-microsoft-com:office:excel'>
              <WindowHeight>8955</WindowHeight>
              <WindowWidth>15195</WindowWidth>
              <WindowTopX>120</WindowTopX>
              <WindowTopY>60</WindowTopY>
              <ProtectStructure>False</ProtectStructure>
              <ProtectWindows>False</ProtectWindows>
             </ExcelWorkbook>
             <Styles>
              <Style ss:ID='Default' 
              ss:Name='Normal'>
               <Alignment ss:Vertical='Bottom'/>
               <Borders/>
               <Font/>
               <Interior/>
               <NumberFormat/>
               <Protection/>
              </Style>
             </Styles>
             <Worksheet ss:Name='Sheet1'>
              <WorksheetOptions xmlns='urn:schemas-microsoft-com:office:excel'>
               <Selected/>
               <ProtectObjects>False</ProtectObjects>
               <ProtectScenarios>False</ProtectScenarios>
              </WorksheetOptions>
             </Worksheet>
              
            </Workbook>"/></object>
              <asp:Button ID="btnPrint" OnClick="btnPrint_Click" runat="server" Text="Print" /> 
    
              <asp:Button ID="btnsave" OnClientClick="javascript: return btnSave_Click();" runat="server" Text="Save" /> 
        </div>
        <input type=hidden runat="server" id="hdnXML" />
        </form>
        
    </body>
    

    Step3:Codebehind-Print Event Function

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

    Reference

    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


    Member
    He is a Team Lead currently working with one of the IT company in India.His overall experience in IT field is 7 years. He is a B.E (computers) graduate from Mumbai University.
    His area of Interest is Microsoft Technology : Asp.Net,C#,Web services,SSIS,SSRS,Windows Workflow Foundation 3.0,SQL server 2005,State Machine Compiler,Regular Expression, Enterprise Library3.0,Spring.net,Design Patterns and Architecture Design.He is MCPD-EA Certified.

    http://santoshpoojari.blogspot.com
    Occupation: Team Leader
    Location: India India

    Other popular ASP.NET articles:

    Article Top
    You must Sign In to use this message board.
    FAQ FAQ 
     
    Noise Tolerance  Layout  Per page   
     Msgs 1 to 2 of 2 (Total in Forum: 2) (Refresh)FirstPrevNext
    Generalimages.. Pinmemberkevdelkevdel7:51 20 May '08  
    GeneralRe: images.. Pinmembersantosh poojari19:42 20 May '08  

    General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

    PermaLink | Privacy | Terms of Use
    Last Updated: 20 May 2008
    Editor:
    Copyright 2008 by santosh poojari
    Everything else Copyright © CodeProject, 1999-2009
    Web10 | Advertise on the Code Project