Click here to Skip to main content
Licence 
First Posted 8 Nov 2006
Views 111,031
Bookmarked 94 times

Display MS Excel Sheets and Charts in ASPX Pages using C#

By | 8 Nov 2006 | Article
How to display an Excel sheet with its format and also charts in an ASPX page.

Introduction

The purpose of this article is to show how to display the exact values in an Excel sheet, with its comments, in an aspx page. The code-behind file is in C#. The method shown here helps to display an Excel sheet with its formatting including the font, color, alignment etc. This will be very useful for developers who do MS Office automation. The pre-requisites are that in the DCOM CONFIG your Excel application should be given permissions to be accessed and loaded in the server system.

Sample Image - ExcelShtAndChrt-In-aspx.jpg

Conversion of the Excel formats require only a few lines of code. The format details include conversion of Excel attributes to .NET attributes, like Excel color to .NET Color.

/// <SUMMARY>
/// Converts Excel Color to Dot Net Color
/// </SUMMARY>
/// Excel Object Color
/// <RETURNS>Returns System.Drawing.Color</RETURNS>
private System.Drawing.Color 
        ConvertExcelColor2DotNetColor(object objExcelColor)
{

    string strColor = "";
    uint uColor = 0;
    int nRed = 0;
    int nGreen = 0;
    int nBlue = 0;

    strColor = objExcelColor.ToString();
    uColor = checked((uint)Convert.ToUInt32(strColor));
    strColor = String.Format("{0:x2}", uColor);
    strColor = "000000" + strColor;
    strColor = strColor.Substring((strColor.Length - 6), 6);

    uColor = 0;
    uColor = Convert.ToUInt32(strColor.Substring(4, 2), 16);
    nRed = (int)uColor;

    uColor = 0;
    uColor = Convert.ToUInt32(strColor.Substring(2, 2), 16);
    nGreen = (int)uColor;

    uColor = 0;
    uColor = Convert.ToUInt32(strColor.Substring(0, 2), 16);
    nBlue = (int)uColor;

    return System.Drawing.Color.FromArgb(nRed, nGreen, nBlue);
}

The format details also include conversion of Excel horizontal alignment to .NET horizontal alignment:

/// <SUMMARY>
/// Converts Excel Horizontal Alignment to DotNet Horizontal Alignment
/// </SUMMARY>
/// Excel Horizontal Alignment
/// <RETURNS>HorizontalAlign</RETURNS>
private HorizontalAlign ExcelHAlign2DotNetHAlign(object objExcelAlign)
{
    switch (((Excel.Range)objExcelAlign).HorizontalAlignment.ToString())
    {
        case "-4131":
            return HorizontalAlign.Left;
        case "-4108":
            return HorizontalAlign.Center;
        case "-4152":
            return HorizontalAlign.Right;
        default:
            return HorizontalAlign.Left;
    }
}

Next is the conversion of Excel vertical alignment to .NET vertical alignment:

/// <SUMMARY>
/// Converts Excel Vertical Alignment to DotNet Vertical Alignment
/// </SUMMARY>
/// Excel Vertical Alignment
/// <RETURNS>VerticalAlign</RETURNS>
private VerticalAlign ExcelVAlign2DotNetVAlign(object objExcelAlign)
{
    switch (((Excel.Range)objExcelAlign).VerticalAlignment.ToString())
    {
        case "-4160":
            return VerticalAlign.Top;
        case "-4108":
            return VerticalAlign.Middle;
        case "-4107":
            return VerticalAlign.Bottom;
        default:
            return VerticalAlign.Bottom;
    }

}

Chart View

Sample Image - ExcelShtAndChrt-In-aspx.jpg

The selection of sheet name will be displayed by "*" delimited. This is because "*" cannot be accepted in Worksheet names.

Problems faced in displaying a Worksheet

  1. Merging of rows is not included because it needs to find out the logic of combining rows (whereas columns merge is possible on insertion as a TableRow).
  2. The chart object is a GIF file, it is generated and put in the server and displayed in an ASPX page. (Here, there is no need to get the chart object inside the page). This is a preliminary trial to put a chart on an ASPX page.

Forthcoming Plans

To display all types of MS Office files in ASPX pages and to produce intelligence on data values etc.

Points to Consider

This meets the business requirements with data display alone and no activities processed. A template of Excel with cell references to other Excel files will give a real time stylesheet data report.

Summary

This page will be enhanced by including several functionalities of MS Excel.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Gnanandam Gopalan

Web Developer

India India

Member

I am basically a C++ Programer done various project for Industrial Automation Activities.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionProblem occured !!!!!!! Pinmemberzimzamzim1:04 20 Sep '11  
AnswerRe: Problem occured !!!!!!! Pinmembermatrixology18:14 27 Nov '11  
GeneralNo display for image PinmemberMember 77855324:51 24 May '11  
GeneralOld format or invalid type library. (Exception from HRESULT: 0x80028018 (TYPE_E_INVDATAREAD)) PinmemberNuclearStorm4:26 16 Feb '11  
GeneralImage in excel is not Displaying Pinmemberam7421:36 29 Mar '10  
GeneralProblem in Displaying Excel PinmemberPallaviwakde0:06 5 Feb '10  
GeneralRe: Problem in Displaying Excel Pinmembersnpcrud9:44 20 Dec '10  
GeneralRe: Problem in Displaying Excel Pinmembersnpcrud10:11 20 Dec '10  
GeneralRe: Problem in Displaying Excel Pinmembermatrixology18:25 27 Nov '11  
GeneralRe: Problem in Displaying Excel Pinmembersnpcrud9:59 28 Nov '11  
GeneralRe: Problem in Displaying Excel Pinmembermatrixology17:04 28 Nov '11  
GeneralNeed same code for VB.Net PinmemberAnOnYmOuS_S2:18 24 Nov '09  
GeneralCool application PinmemberLulama21:55 8 Mar '09  
GeneralNice Application Pinmemberatheequepasha18:14 12 Nov '08  
Generaltemp gif file PinmemberMember 39178105:24 22 Sep '08  
GeneralLoading takes a long time PinmemberAgnes Loyola3:29 16 Jul '08  
GeneralDisplay Excel inside the aspx page PinmemberAgnes Loyola20:56 11 Jul '08  
GeneralTo display excel sheet PinmemberAgnes Loyola1:24 11 Jul '08  
AnswerRe: To display excel sheet PinmemberGnanandam Gopalan2:46 11 Jul '08  
GeneralRe: To display excel sheet PinmemberAgnes Loyola3:06 11 Jul '08  
Questionhow to load the Excel sheet in the page load PinmemberAgnes Loyola1:05 10 Jul '08  
AnswerRe: how to load the Excel sheet in the page load PinmemberGnanandam Gopalan1:14 10 Jul '08  
GeneralThanks for nice Example, But I am getting one errors [modified] PinmemberMember 204885620:50 26 Feb '08  
GeneralNice PinmemberPooya Musavi8:00 19 Oct '07  
GeneralRe: Nice PinmemberGnanandam Gopalan23:11 21 Oct '07  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web01 | 2.5.120517.1 | Last Updated 9 Nov 2006
Article Copyright 2006 by Gnanandam Gopalan
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid