How to use the spreadsheet Web Component with Visual Basic

zhaozj2021-02-16  83

How to use the spreadsheet Web Component with Visual Basic

Suitable

This Article Was previously distribLished Under Q235883

SUMMARYIf you want to use a spreadsheet or calculation engine in your custom Visual Basic solution, you can use the Spreadsheet control included with the Microsoft Office Web Components. This article discusses how to insert a spreadsheet as a control on a Visual Basic form and populate the Spreadsheet with data.

More informationYou can add the office web company in the same war...............................

Spreadsheet Control Included with The Office Web Components IS A More Robust Option Than THE

DataGrid Control That Ships with Visual Basic. Use the spreadsheet data to display formatted data, Calculate Formulas and allow user interactivity.

IMPORTANT: To view and work with any of the Office Web Components, you must have them installed on your computer The components are installed with Microsoft Office 2000, or, if your company has an Office 2000 site license, the components can be downloaded from. Your intranet. Your Users Must Have these Components Installed As Well, And Must Be Covered by An Office 2000 License. You and your users Must Use Microsoft Internet Explorer Version 4.01 or Later.

Steps to create a Sample Project

Create a new Standard EXE project in Visual Basic. Form1 is created by default. Add a reference to Microsoft Office Web Components 9.0. Add the Spreadsheet control from the Visual Basic toolbox to your form. The Spreadsheet control can be displayed and used like a worksheet , or it can be hidden and use as a calculation engine for visible controls on the point.working with the

Spreadsheet control in Visual Basic is almost identical to working with a worksheet in Excel. The methods and properties you use to manipulate the Spreadsheet component are similar to those you would use when automating Excel.Populating a Spreadsheet ControlYou can add data to a

Spreadsheet Control in One of the Following Ways:

Enter data directly into the grid. Copy and paste data from an Excel sheet, text file, or Word document. Import data from a text file or a Web page. For more information see the LoadText Method topic and the HTMLURL Property topic in online Help Write code to populate the spreadsheet with data.note That there is no way to bind the

Spreadsheet Control to Data Directly.

The Following Procedure Populates and Formats A

Spreadsheet Control with data from the sample database northwind (nwind.mdb):

Sub GetNwindData () Dim rstEmployees As Object Dim cnn As String Dim strSQL As String Dim fldCount As Integer Dim intIRow As Integer Dim intICol As Integer Dim varData As Variant 'Create recordset and fill with records from Northwind sample database. Set rstEmployees = CreateObject ( " ADODB.Recordset ") cnn =" DRIVER = {Microsoft Access Driver (* .mdb)}; DBQ = C: / Program Files / Microsoft Visual Studio / VB98 / nwind.mdb "strSQL =" SELECT FirstName, LastName, Title, Extension fROM Employees ORDER BY LastName "rstEmployees.Open strSQL, cnn, 3 'adOpenStatic = 3' Clear any existing values ​​from the spreadsheet control. Spreadsheet1.ActiveSheet.Cells (1, 1) .Select Spreadsheet1.ActiveSheet.UsedRange.Clear 'Add the . field names as column headers For fldCount = 0 To rstEmployees.Fields.Count - 1 intIRow = intIRow 1 Spreadsheet1.ActiveSheet.Cells (1, intIRow) .Value = rstEmployees.Fields (fldCount) .Name Next 'Fill the control with Data from The Database. Dim inumcols as inum inumrows as integer inumcols = RstEmployees.Fields.Count iNumRows = rstEmployees.Recordcount varData = rstEmployees.GetRows (iNumRows) For intIRow = 1 To iNumRows For intICol = 1 To iNumCols Spreadsheet1.ActiveSheet.Cells (intIRow 1, intICol) .Value = varData (intICol - 1, intIRow -. 1) Next Next 'Format the headers in row 1 with a Bold Font that is 11 points with Spreadsheet1.ActiveSheet.Range (Spreadsheet1.Cells (1, 1), Spreadsheet1.ActiveSheet.Cells (1, iNumCols) ) .Font .bold = true .size = 11 end with '

AutoFit the columns and make all text left-aligned. With Spreadsheet1.ActiveSheet.Range (Spreadsheet1.Cells (1, 1), Spreadsheet1.ActiveSheet.Cells (iNumRows 1, iNumCols)) .AutoFitColumns .HAlignment = ssHAlignLeft End With End SubNote That a spreadsheet control is not a workbook and contacts Only a single sheet. The Activesheet Property Always Refers To this Single Sheet.

Getting helpafter You create a reason to the office web company ibrary, you can access help through the object browser or from

General Tab of the Spreadsheet Property Toolbox. You Can Also Locate and Double-Click The Help File Msowcvba.chm. To Get Help ON A Specific Keyword, Highlight The Keyword and Press The

F1 Key. For Information About Creating References and Using Object Model Help, See Help In Visual Basic or Visual Basic for Applications.

You can Bind A Chart Control to a Spreadsheet Control. For Additional Information on The Chart Control, please click The article Number Below to View The Article in The Microsoft Knowledge Base:

235885 How to use the chart Web Component with VB

Referencesfor More Information About Office Web Components, See Chapter 12, "Using Web Technologies," In The Microsoft Office 2000 / Visual Basic Programmer's Guide.

The Information in this Article Applies TO:

Microsoft Office Spreadsheet Component 9.0

Last Reviewed: 7/14/2004 (1.2) Keywords: kbhowto kbofficeWebspread KB235883

转载请注明原文地址:https://www.9cbs.com/read-14646.html

New Post(0)