I've talked about using Excel, OWC SpreadSheet and the OWC Chart class from ASP.NET in several other articles here. There's one last item I never covered, and it's an important one, now that Office XP (2002) is in more widespread USE.
The Office Web Components are lightweight COM components that are free-threaded and designed to be used on a web server. One important use, however, does not involve showing a visible Spreadsheet in your ASP.NET web applications. This is when you may decide that you have an Excel workbook that does some pretty heavy-duty calculations, and you would prefer to keep the formulas and all your hard work in Excel, and leverage the power of Excel to do the calculations for the web from an ASP.NET WebForms Page.
Perhaps, for example, you have a bunch of input data that comes from a database (maybe it was a survey or other WebForm - based application), and now the entered data are stored in a database You may wish to bring this data out. Plug It Into The Appropriate Places In Your Marvelous Excel Spreadsheet Creament, Calculate Results and Display The Web User On Demand.
With Office Web Components (OWC) you do not have to incur the heavy penalty of instantiating a very large COM Server (Microsoft Excel) through Interop and then deal with all the marshalling constructs and finally (if you are lucky) get the little unhoustrained puppies to unload when you are done and not have your systems admin come to you the next day asking why he had to reboot a production web server because there were 123 separate instances of "Excel.exe" showing in the task manager .... in fact, you do not need Excel to be installed on the server at all, just the lightweight OWC package which takes about 2 minutes to install and has no drag on performance whatever in a production webserver unless it is actually used.To show you how simple it can be to use an Excel spreadsheet with OWC, I created a simple "Loan Calculator" spreadsheet with input cells for Principal, Term, and Interest Rate, and an output cell that uses the Excel PMT () function to compute the monthly payment WE THEN S ave this workbook using the "XML Spreadsheet" option available on Excel 2002 and above. This is the only format that the OWC Spreadsheet Component can read and bring in all the formulas, etc. Do not attempt to load a regular XLS file in OWC, IT can't be done, period.
Then, in my VB.NET ASP.NET application's button click, I have this simple code to enter the user's WebForm entries into the OWC Spreadsheet class, perform the calculation, and pull out the result and display it to the user:
Private Sub Button1_Click (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.ClickDim sp As OWC10.SpreadsheetClass = New SpreadsheetClasssp.XMLURL = MapPath ( "test.xml") sp.Range ( "B1: B4" ) .Cells (1) .value = convert.TOINT32 (txtprincipal.text) sp.range ("b1: b4"). Cells (2) .value = convert.toint32 (txtterm.text) sp.range ("b1: B4 "). Cells (3) .value = convert.TOINT32 (txtrate.text) sp.calculateFull () lblMessage.Text =" Monthly Payment: "& (sp.range (" B1: B4 "). Cells (4) .Value * -1) End SubPretty simple, would not you agree And guess what -? even though I did not even bother to explicitly call the ReleaseCOMObject methods, there is no errant copy of anything left around in memory These components are. all freethreaded. you do not even have to set "ASPCOMPAT" on your page, so there's no performance drain. I have not tried it, but I suspect that since it's freethreaded, you could even store a fully instantiated Spreadsheet class Instance Holding a Complete Workbook in Application Scope.
Obviously, this is a very simple example. However, there is nothing to stop you from loading the most complex Workbooks that have been saved as XML Spreadsheets and performing heavy-duty calculations in your web reporting pages! OWC Spreadsheet class supports all the formulas, Everything except pivot tables and charts.
The Downloadable Solution, Which IS in Visual Studio.net 2003 Format, Includes a Copy of The Test.xml Spreadsheet Used Above.
Download The Code Accompanying this Article