一步一步SharePoint 2007之四十八:实现Excel Service(3)——调用Excel Service

摘要
  本篇文章将介绍实现Excel Service的最后一部分——调用Excel Service。
  为了方便您的学习,您可以下载本篇文章所创建的工程。单击此处下载。
正文
  下面将记录每一步的操作过程。
  1、首先打开Visual Studio 2005,创建一个ASP.NET Web Application项目。
请单击查看原始尺寸的图片。

  2、创建好的项目就是这个样子的。
请单击查看原始尺寸的图片。

  3、在Visual Studio 2005的菜单中依次选择Project,Add Web Reference,并单击该菜单项。
请单击查看原始尺寸的图片。

  4、在弹出的Add Web Reference页面中,在URL文本框中输入上一篇文章中访问过的Web Service的网址:http://denny:9001/sites/Wodeweb/Docs/ExcelServices/_vti_bin/ExcelService.asmx,并在Web reference name文本框中输入一个名字。然后单击Add Reference按钮。
请单击查看原始尺寸的图片。

  5、添加完Web Reference后的项目就是这样的了。
请单击查看原始尺寸的图片。

  6、打开Default.aspx页面的设计试图,并按照图中的样子在该页面上添加几个Label框、TextBox框和Button按钮,并分别命名。
请单击查看原始尺寸的图片。

  7、双击Button按钮,即可进入编写代码的试图,我们将代码更改为如下的代码:

     1 using System;

     2 using System.Data;

     3 using System.Configuration;

     4 using System.Collections;

     5 using System.Web;

     6 using System.Web.Security;

     7 using System.Web.UI;

     8 using System.Web.UI.WebControls;

     9 using System.Web.UI.WebControls.WebParts;

    10 using System.Web.UI.HtmlControls;

    11

    12 using System.Net;

    13

    14 using Eallies.ExcelServices.Demo.WebService;

    15

    16 namespace Eallies.ExcelServices.Demo

    17 {

    18     public partial class _Default : System.Web.UI.Page

    19      {

    20         protected void Page_Load(object sender, EventArgs e)

    21          {

    22

    23          }

    24

    25         protected void btnCalculate_Click(object sender, EventArgs e)

    26          {

    27             string strWorkbookPath = @"http://denny:9001/sites/Wodeweb/Docs/ExcelServices/工资计算.xlsx";

    28             string strSheetName = "Sheet1";

    29             string strCultureName = "zh-cn";

    30             Status[] status = default(Status[]);

    31

    32             ExcelService es = new ExcelService();

    33              es.Credentials = CredentialCache.DefaultCredentials;

    34

    35             string strSessionId = es.OpenWorkbook(strWorkbookPath, strCultureName, strCultureName, out status);

    36

    37              es.SetCellA1(strSessionId, strSheetName, "C2", this.txtC2.Text);

    38              es.SetCellA1(strSessionId, strSheetName, "C3", this.txtC3.Text);

    39              es.SetCellA1(strSessionId, strSheetName, "C4", this.txtC4.Text);

    40

    41             string strResult = (string)es.GetCellA1(strSessionId, strSheetName, "C6", true, out status);

    42

    43             if (string.IsNullOrEmpty(strResult) == false)

    44              {

    45                 this.lblC6.Text = strResult;

    46              }

    47          }

    48      }

    49 }

请单击查看原始尺寸的图片。

  8、至此,项目创建完毕,保存并运行该项目。
  在各个文本框中输入一些数字,然后单击“计算”按钮。


  9、这时,“实发”Label框后就已经出现了计算结果。


  至此,实现Excel Service就成功了,恭喜恭喜!
  当然,这只是一个非常简单的例子,朋友们在实现这个Excel Service后,就可以根据自己的需要,尝试更为复杂的Excel Service了。
  后面的文章中,笔者将讲到更为精彩的内容,欢迎大家继续关注:)多谢

posted on 2007-10-24 11:31  执法长老  阅读(225)  评论(1编辑  收藏  举报

导航