Click here to Skip to main content
11,576,993 members (56,722 online)
Click here to Skip to main content

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

, 8 Nov 2006 157.7K 5.4K 103
Rate this:
Please Sign up or sign in to vote.
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.

/// <span class="code-SummaryComment"><SUMMARY>
</span>
/// Converts Excel Color to Dot Net Color
/// <span class="code-SummaryComment"></SUMMARY>
</span>
/// Excel Object Color
/// <span class="code-SummaryComment"><RETURNS>Returns System.Drawing.Color</RETURNS>
</span>
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:

/// <span class="code-SummaryComment"><SUMMARY>
</span>
/// Converts Excel Horizontal Alignment to DotNet Horizontal Alignment
/// <span class="code-SummaryComment"></SUMMARY>
</span>
/// Excel Horizontal Alignment
/// <span class="code-SummaryComment"><RETURNS>HorizontalAlign</RETURNS>
</span>
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:

/// <span class="code-SummaryComment"><SUMMARY>
</span>
/// Converts Excel Vertical Alignment to DotNet Vertical Alignment
/// <span class="code-SummaryComment"></SUMMARY>
</span>
/// Excel Vertical Alignment
/// <span class="code-SummaryComment"><RETURNS>VerticalAlign</RETURNS>
</span>
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

Share

About the Author

Gnanandam Gopalan
Web Developer
India India
I am basically a C++ Programer done various project for Industrial Automation Activities.

You may also be interested in...

Comments and Discussions

 
Questionhey bro reaply me fast i need solution of this error... Pin
ntc994-Nov-14 21:04
memberntc994-Nov-14 21:04 
GeneralMy vote of 5 Pin
Asif Iqbal A.Khan5-Aug-13 1:35
memberAsif Iqbal A.Khan5-Aug-13 1:35 
QuestionProblem occured !!!!!!! Pin
zimzamzim20-Sep-11 1:04
memberzimzamzim20-Sep-11 1:04 
AnswerRe: Problem occured !!!!!!! Pin
matrixology27-Nov-11 18:14
membermatrixology27-Nov-11 18:14 
GeneralNo display for image Pin
Member 778553224-May-11 4:51
memberMember 778553224-May-11 4:51 
I compiled and ran the code. I proceeded to select the PopulationSample ->List ->Chart0-14 Population Chart. The graphical image does not appear, however there is a small icon instead. I used the 2010 Visual studio and 2007 Office
Thank you in advance for advice
Regards,
KJ
GeneralOld format or invalid type library. (Exception from HRESULT: 0x80028018 (TYPE_E_INVDATAREAD)) Pin
NuclearStorm16-Feb-11 4:26
memberNuclearStorm16-Feb-11 4:26 
GeneralImage in excel is not Displaying Pin
am7429-Mar-10 21:36
memberam7429-Mar-10 21:36 
GeneralProblem in Displaying Excel Pin
Pallaviwakde5-Feb-10 0:06
memberPallaviwakde5-Feb-10 0:06 
GeneralRe: Problem in Displaying Excel Pin
snpcrud20-Dec-10 9:44
membersnpcrud20-Dec-10 9:44 
GeneralRe: Problem in Displaying Excel Pin
snpcrud20-Dec-10 10:11
membersnpcrud20-Dec-10 10:11 
GeneralRe: Problem in Displaying Excel Pin
matrixology27-Nov-11 18:25
membermatrixology27-Nov-11 18:25 
GeneralRe: Problem in Displaying Excel Pin
snpcrud28-Nov-11 9:59
membersnpcrud28-Nov-11 9:59 
GeneralRe: Problem in Displaying Excel Pin
matrixology28-Nov-11 17:04
membermatrixology28-Nov-11 17:04 
GeneralNeed same code for VB.Net Pin
AnOnYmOuS_S24-Nov-09 2:18
memberAnOnYmOuS_S24-Nov-09 2:18 
GeneralCool application Pin
Lulama8-Mar-09 21:55
memberLulama8-Mar-09 21:55 
GeneralNice Application Pin
atheequepasha12-Nov-08 18:14
memberatheequepasha12-Nov-08 18:14 
Generaltemp gif file Pin
Member 391781022-Sep-08 5:24
memberMember 391781022-Sep-08 5:24 
GeneralLoading takes a long time Pin
Agnes Loyola16-Jul-08 3:29
memberAgnes Loyola16-Jul-08 3:29 
GeneralDisplay Excel inside the aspx page Pin
Agnes Loyola11-Jul-08 20:56
memberAgnes Loyola11-Jul-08 20:56 
GeneralTo display excel sheet Pin
Agnes Loyola11-Jul-08 1:24
memberAgnes Loyola11-Jul-08 1:24 
AnswerRe: To display excel sheet Pin
Gnanandam Gopalan11-Jul-08 2:46
memberGnanandam Gopalan11-Jul-08 2:46 
GeneralRe: To display excel sheet Pin
Agnes Loyola11-Jul-08 3:06
memberAgnes Loyola11-Jul-08 3:06 
Questionhow to load the Excel sheet in the page load Pin
Agnes Loyola10-Jul-08 1:05
memberAgnes Loyola10-Jul-08 1:05 
AnswerRe: how to load the Excel sheet in the page load Pin
Gnanandam Gopalan10-Jul-08 1:14
memberGnanandam Gopalan10-Jul-08 1:14 
GeneralThanks for nice Example, But I am getting one errors [modified] Pin
Member 204885626-Feb-08 20:50
memberMember 204885626-Feb-08 20:50 
GeneralNice Pin
Pooya Musavi19-Oct-07 8:00
memberPooya Musavi19-Oct-07 8:00 
GeneralRe: Nice Pin
Gnanandam Gopalan21-Oct-07 23:11
memberGnanandam Gopalan21-Oct-07 23:11 
QuestionVB Version Pin
smcirish17-Oct-07 4:19
membersmcirish17-Oct-07 4:19 
GeneralProblem in Horizontal Alignment. Pin
Anees Mhd25-Sep-07 1:13
memberAnees Mhd25-Sep-07 1:13 
GeneralRe: Problem in Horizontal Alignment. Pin
Anees Mhd25-Sep-07 17:48
memberAnees Mhd25-Sep-07 17:48 
GeneralYour Application is greate but ... Pin
hishammm17-May-07 2:57
memberhishammm17-May-07 2:57 
GeneralRe: Your Application is greate but ... Pin
Gnanandam Gopalan20-May-07 23:15
memberGnanandam Gopalan20-May-07 23:15 
GeneralRe: Your Application is greate but ... Pin
shahzad_mca11-Oct-07 6:20
membershahzad_mca11-Oct-07 6:20 
QuestionExcel process not closed Pin
sandu200415-Nov-06 20:36
membersandu200415-Nov-06 20:36 
AnswerRe: Excel process not closed Pin
Gnanandam Gopalan17-Nov-06 3:28
memberGnanandam Gopalan17-Nov-06 3:28 
GeneralRe: Excel process not closed Pin
sandu200417-Nov-06 5:00
membersandu200417-Nov-06 5:00 
GeneralRe: Excel process not closed Pin
Gnanandam Gopalan19-Nov-06 20:04
memberGnanandam Gopalan19-Nov-06 20:04 
GeneralProblem open the Excel Sheet Pin
J@vi Martinez9-Nov-06 5:32
memberJ@vi Martinez9-Nov-06 5:32 
AnswerRe: Problem open the Excel Sheet Pin
Gnanandam Gopalan9-Nov-06 18:21
memberGnanandam Gopalan9-Nov-06 18:21 
GeneralRe: Problem open the Excel Sheet Pin
J@vi Martinez9-Nov-06 20:10
memberJ@vi Martinez9-Nov-06 20:10 
GeneralRe: Problem open the Excel Sheet Pin
J@vi Martinez14-Nov-06 0:43
memberJ@vi Martinez14-Nov-06 0:43 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150603.1 | Last Updated 9 Nov 2006
Article Copyright 2006 by Gnanandam Gopalan
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid