如何:以编程方式向页面添加 Excel Web Access Web 部件
此示例演示如何以编程方式将一个 Excel Web Access Web 部件添加到 SharePoint 页面。它还演示如何在 Excel Web Access Web 部件中以编程方式显示 Excel 工作簿。
下面的项目使用 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 的引用
在“项目”菜单上,单击“添加引用”。
在“添加引用”对话框中,单击“浏览”。
备注
还可以在“解决方案资源管理器”窗格中打开“添加引用”对话框,方法是右键单击“引用”,然后选择“添加引用”。
浏览到 Microsoft.Office.Excel.WebUI.dll 的位置。
选择“Microsoft.Office.Excel.WebUI.dll”,然后单击“确定”。
单击“添加引用”,对 Microsoft.Office.Excel.WebUI.dll 的引用即被添加到项目中。
实例化 Web 部件
实例化 Excel Web Access Web 部件
将 Microsoft.Office.Excel.WebUI 命名空间作为指令添加到代码中,以便当您使用此命名空间中的类型时不必完全限定这些类型:
using Microsoft.Office.Excel.WebUI;
实例化并初始化 Excel Web Access Web 部件,如下所示:
// Instantiate ExcelWebRenderer class ExcelWebRenderer ewaWebPart = new ExcelWebRenderer();
以编程方式显示工作簿
在此示例中,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 中,可以通过右键单击工作簿并选择“复制快捷方式”来获得工作簿的路径。此外,也可以选择“属性”并从中复制工作簿的路径。
可以通过使用下面的代码以编程方式显示 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