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

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.

< PRE lang = cs id = pre0 style = " MARGIN-TOP: 0px; DISPLAY: block "  nd = " 11 " > /// <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(42), 16);
    nRed 
= (int)uColor;

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

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

    
return System.Drawing.Color.FromArgb(nRed, nGreen, nBlue);
}
</ PRE >

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.

<script src="/script/togglePre.js" type="text/javascript"></script>

About Gnanandam Gopalan


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

Click here to view Gnanandam Gopalan's online profile.

 Download source files - 413 Kb

NOTE:

Please check whether Excel Application (i.e. appOP) has an instance in Web Application.
If not, type "dcomcnfg" in Run of Start Menu, Select DCOM Config in Component Services, Right Click on "Microsoft Excel Application" and select Properties, Go to Security Tab in Launch and Activation Permissions -> select Customize to enter, Give permission for Machine Name/ASPNET and Machine Name/IIS_WPG for Local Launch and Local Activation.

This permission is for loading MS Excel Application in the memory of IIS and if a particular sheet is not opening, may be naming pattern is not matching with string variable. Please check this, and revert back me.

Have a good Luck

this from :http://www.codeproject.com/aspnet/ExcelShtAndChrt-In-aspx.asp

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值