The O.W.C does not work correctly when used with VS 2005. This blog will provide a work-around of sorts. If you are not satisfied with this solution, you may consider switching to VSTO 2005 and thin clients to provide Office functionality. I'll blog more about this exciting approach at a later date, but for now, this fix is geared toward web pages.
One way is to embed the object tag in the Internet browser. Currently, there is no support for the OWC outside of Internet Explorer. Here is an example. Create a web project and open the aspx file. Embed the OWC object tag between the form tag.
<form>
<object classid='clsid:0002E559-0000-0000-C000-00000000004 6' id='sp' ></object>
</form>
If you switch to design view at this point, the spreadsheet will present as a tiny X. You can ignore it. When you run the application, the spreadsheet will show up normally. If you attempt to set design-time features by right-clicking on the tiny X, VS 2005 may crash unexpectedly.
A final option is to embed the object tag in the code-behind and stream it out to the browser. Here is a complete example.
using System;
public partial class _Default : System.Web.UI.Page
{
string spreadsheet = "<object classid='clsid:0002E559-0000-0000-C000-00000000004 6' id='sp' ></object>";
public string CustomizeSpreadSheet()
{
System.Text.StringBuilder scripter = new System.Text.StringBuilder();
//it's safe to use document.all here since this can only run in IE
scripter.Append("\n<script language='javascript'>");
scripter.Append("\nif(document.all.sp != null){");
scripter.Append("\ndocument.all.sp.ActiveWindow.FreezePanes = true");
scripter.Append("\ndocument.sp.columns(2).hidden = true");
scripter.Append("\n}\n</script>");
return scripter.ToString();
}
public string LoadData(string payload)
{
System.Text.StringBuilder script = new System.Text.StringBuilder();
//it's safe to use document.all here since this can only run in IE
script.Append("\n<script language='javascript'>function loadData(){");
script.Append("\nif(document.all.sp != null){");
script.Append("\ndocument.all.sp.CSVDATA=").Append(payload);
script.Append("\n}\n}\n</script>");
return script.ToString();
}
protected void Page_Load(object sender, EventArgs e)
{
Response.Write(spreadsheet);
Response.Write(LoadData("'1,2,3,4,5\\n6,7,8,9'"));
Response.Write("<script>\nloadData();</script>");
Response.Write(CustomizeSpreadSheet());
}
}
The code is fairly easy to understand. The customizespreadsheet routine simply adds some customization to the spreadsheet. The loaddata routine, fills the spreadsheet with data. Once these routines are called, data will be loaded and customization will be implemented on the server as a string. The string is streamed to the client and loaded in the browser to display the spreadsheet.
Until MS fixes the design-time bug, there is no way to actually allow the OWC control to be visible in the browser. Consequently, you will not be able to customize the controls at design-time. However, as the example demonstrates, you can easily add those customizations at run-time.
The code presents bare-bones functionality. You are free to use other constructs available to stream script out to the browser. Finally, I've used only the spreadsheet in this example, however, the other components