次の方法で共有


[方法] Excel Web Access Web パーツをプログラムによってページに追加する

この例では、Excel Web Access Web パーツをプログラムによって SharePoint ページに追加する方法を示します。また、Excel ブックをプログラムによって Excel Web Access Web パーツに表示する方法も示します。

以下のプロジェクトでは、Microsoft Visual Studio 2005 を使用します。

注意

Visual Studio 統合開発環境 (IDE) で使用する設定によっては、プロジェクトの作成処理がわずかに異なる可能性があります。

注意

既に SharePoint ドキュメント ライブラリを作成し、それを信頼できる場所にしたことを前提としています。この詳細については、「[方法] 場所を信頼する」および「[方法] スクリプトを使用してブックの場所を信頼する」を参照してください。

参照を追加する

次の手順では、Microsoft.Office.Excel.WebUI.dll を検索し、参照を追加する方法を示します。

注意

Microsoft.Office.Excel.WebUI.dll をグローバル アセンブリ キャッシュから任意のフォルダに既にコピーしたことを前提としています。Microsoft.Office.Excel.WebUI.dll を検索し、コピーする方法の詳細については、「[方法] Microsoft.Office.Excel.WebUI.dll を見つけてコピーする」を参照してください。

Microsoft.Office.Excel.WebUI.dll に参照を追加するには

  1. [プロジェクト] メニューで、[参照の追加] をクリックします。

  2. [参照の追加] ダイアログ ボックスで、[参照] をクリックします。

    注意

    [ソリューション エクスプローラ] ウィンドウで、[参照設定] を右クリックし、[参照の追加] を選択して、[参照の追加] ダイアログ ボックスを開くこともできます。

  3. Microsoft.Office.Excel.WebUI.dll の場所を参照します。

  4. Microsoft.Office.Excel.WebUI.dll を選択し、[OK] をクリックします。

  5. [参照の追加] をクリックします。Microsoft.Office.Excel.WebUI.dll への参照がプロジェクトに追加されます。

Web パーツをインスタンス化する

Excel Web Access Web パーツをインスタンス化するには

  1. Microsoft.Office.Excel.WebUI 名前空間をディレクティブとしてコードに追加し、この名前空間で型を使用するときに、完全修飾名を指定しなくても済むようにします。

    using Microsoft.Office.Excel.WebUI;
    
  2. Excel Web Access Web パーツを次のとおりインスタンス化し、初期化します。

    // Instantiate ExcelWebRenderer class
     ExcelWebRenderer ewaWebPart = new ExcelWebRenderer();
    

プログラムによってブックを表示するには

  1. 次の例では、AddWebPart メソッドは Excel ブックへのパスを引数としてとります。ユーザーは、Windows フォームのテキスト ボックスに入力し、ボタンをクリックしてパスを指定します。

    public bool AddWebPart(string sitename, string book)
    {
    ...
    }
                private void AddEWAButton_Click(object sender, 
                    EventArgs e)
                {
                    siteurl = textBox1.Text;
                    bookuri = textBox2.Text;
                    succeeded = AddWebPart(siteurl, bookuri);
                    if (succeeded)
                    {
                        MessageBox.Show(
                            success,
                            appname,
                            MessageBoxButtons.OK,
                            MessageBoxIcon.Information);
                        progressBar1.Value = 1;
                    }
                }
    

    重要

    ブックが信頼できる場所に保存されていることを確認します。

    注意

    ブックを右クリックし、[ショートカットのコピー] を選択して、Microsoft Office SharePoint Server 2007 のブックへのパスを取得することができます。または、[プロパティ] を選択し、その場所からブックへのパスをコピーすることができます。

  2. 次のコードを使用して、Excel ブックをプログラムによって表示することができます。

    using Microsoft.Office.Excel.WebUI;
    namespace AddEWATool
    {
        public partial class Form1 : Form
        {
             ...
    
               /// <param name="sitename">URL of the 
               ///Windows SharePoint Services site</param>
               /// <param name="book">URI to the workbook</param>
            public bool AddWebPart(string sitename, string book)
            {
                ...
                ExcelWebRenderer ewaWebPart = new ExcelWebRenderer();
                ewaWebPart.WorkbookUri = book;
    

using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace AddEWATool
{
    /// <summary>
    /// Program class 
    /// </summary>
    internal static class Program
    {
        /// <summary>
        /// The main entry point for the application. 
        /// </summary>
        /// <param name="args">arguments</param>
        /// <returns>int</returns>
        [STAThread]
        public static int Main(string[] args)
        {
            //Application.EnableVisualStyles();
            if (args.Length == 0)
            {
                Application.Run(new Form1());
                return 1;
            }
            else 
            {
                Commandline comm = new Commandline();
                int worked = comm.CommandLineAddWebPart(args);
                return worked;
            }
        }
    }
} 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.Office.Excel.WebUI;
namespace AddEWATool
{
    /// <summary>
    /// Form1 class derived from System.Windows.Forms
    /// </summary>
    public partial class Form1 : Form
    {
        private string siteurl;
        private string bookuri;
        private bool succeeded;

        private string appname = "AddEWATool";
        private string specifyinput = "Please add a site URL, 
            for example, http://myserver/site/";
        private string siteproblem = "There was a problem with
            the site name. Please check that the site exists.";
        private string addproblem = "There was a problem adding the 
            Web Part.";
        private string success = "Web Part successfully added.";
        private SPSite site;
        private SPWeb TargetWeb;
        private SPWebPartCollection sharedWebParts;
        /// <summary>
        /// Class Constructor
        /// </summary>
        public Form1()
        {
            InitializeComponent();
        }
        /// <summary>
        /// Method to add the Excel Web Access Web Part
        /// </summary>
        /// <param name="sitename">URL of the 
        ///Windows SharePoint Services site</param>
        /// <param name="book">URI to the workbook</param>
        /// <returns>bool</returns>"
        public bool AddWebPart(string sitename, string book)
        {
            bool b = false;
            progressBar1.Visible = true;
            progressBar1.Minimum = 1;
            progressBar1.Maximum = 4;
            progressBar1.Value = 1;
            progressBar1.Step = 1;

            if (String.IsNullOrEmpty(sitename))
            {
                MessageBox.Show(
                    specifyinput,
                    appname,
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Asterisk);
                return b;
            }
                try
            {
                site = new SPSite(sitename);
                TargetWeb = site.OpenWeb();
            }
            catch (Exception exc)
            {
                MessageBox.Show(
                    siteproblem + "\n" + exc.Message,
                    appname,
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Asterisk);
                    progressBar1.Value = 1;
                 return b;
                }
                progressBar1.PerformStep();
                //Get the collection of shared Web Parts 
                //on the home page
                //Log.Comment("Get the collection of 
                //personal Web Parts on default.aspx");
                try
                {
                   sharedWebParts = 
                     TargetWeb.GetWebPartCollection("Default.aspx", 
                     Microsoft.SharePoint.WebPartPages.Storage.Shared);
                }
                catch (Exception exc)
                {
                    MessageBox.Show(
                        siteproblem + "\n" + exc.Message,
                        appname,
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Asterisk);
                    progressBar1.Value = 1;
                    return b;
                }
                progressBar1.PerformStep();
                //Instantiate Excel Web Access Web Part
                //Add an Excel Web Access Web Part in a shared view
                ExcelWebRenderer ewaWebPart = new ExcelWebRenderer();
                progressBar1.PerformStep();
                ewaWebPart.ZoneID = "Left";
                ewaWebPart.WorkbookUri = book;
                try
                {
                    sharedWebParts.Add(ewaWebPart);
                }
                catch (Exception exc)
                {
                    MessageBox.Show(
                        addproblem + "\n" + exc.Message,
                        appname,
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Asterisk);
                    progressBar1.Value = 1;
                    return b;
                }
                    progressBar1.PerformStep();
                    b = true;
                    return b;
                }
                /// <summary>
                /// Button1 click handler
                /// </summary>
                /// <param name="sender">caller</param>
                /// <param name="e">event</param>
            private void AddEWAButton_Click(object sender, 
                EventArgs e)
            {
                siteurl = textBox1.Text;
                bookuri = textBox2.Text;
                succeeded = AddWebPart(siteurl, bookuri);
                if (succeeded)
                {
                    MessageBox.Show(
                        success,
                        appname,
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Information);
                    progressBar1.Value = 1;
                }
            }
        
    } 

堅牢なプログラミング

使用する Excel ブックは信頼できる場所になければなりません。

See Also

タスク

[方法] スクリプトを使用してブックの場所を信頼する

[方法] Microsoft.Office.Excel.WebUI.dll を見つけてコピーする

概念

Excel Services の通知

Excel Services の既知の問題とヒント