vb窗口使用excel)

  • 格式:doc
  • 大小:495.00 KB
  • 文档页数:49

OWC使用方法OWC==> Office Web Components是微软Office的轻量级开发工具,随Office安装时一起安装Office2003版本对应OWC11,里面有5个控件对象图表工作区对象数据源控件对象记录集控件对象数据透视表列表对象电子表格对象很久以前用过的,最近有人问就翻出来看看其实很简单,只是可能没有到注意文档罢了我简单用VB做了下常用的SpreadSheet例子Form上加一个Spreadsheet控件代码,很简单,没什么好说的Private Sub Form_Load()'单元格'可以使用循环的方式填充单元格Spreadsheet1.Cells(1, 1) = "第1个格"Spreadsheet1.Cells(1, 5) = "第5个格"'区域'合并Spreadsheet1.Range("A3:D3").MergeSpreadsheet1.Range("A3:D3").Value = "合并效果"'格式设置Spreadsheet1.Range("A3:D3") = "黑体"Spreadsheet1.Range("A3:D3").Font.Size = 15Spreadsheet1.Range("A3:D3").Borders(xlEdgeTop).Color = vbRedSpreadsheet1.Range("A3:D3").Borders(xlEdgeBottom).Color = vbRedSpreadsheet1.Range("A3:D3").Borders(xlEdgeLeft).Color = vbRedSpreadsheet1.Range("A3:D3").Borders(xlEdgeRight).Color = vbRed'' '不提示直接保存为本地文件'' Spreadsheet1.Export "c:\xxx.xls", ssExportActionNone'' '直接在Excel中打开'' Spreadsheet1.Export "c:\xxx.xls", ssExportActionOpenInExcelEnd Sub运行出来就是这个样子因为整个编程中使用了VBA语法,所以还是很容易理解的如果你的机器上安装了Office2000/xp/2003那么你的机器上已经有使用文档及开发文档了,很详尽,参照一下,应该没啥问题。

我的机器装的是2003,在如下位置上有相关文档C:\Program Files\Common Files\Microsoft Shared\Web Components\10\2052C:\Program Files\Common Files\Microsoft Shared\Web Components\11\2052前面的文档都是使用帮助OWCVBA11.CHM 是开发文档ChartSpace对象:图形容器对象,也是顶层对象。

如果要使用OWC绘制图形,至少要创建一个ChartSpace对象。

所谓容器,就是说ChartSpace 对象中可以包含多个图形对象,最多16个。

ChChart对象、ChCharts集合、ChCharts.Add()方法:ChChart是图形对象。

一个柱状图、饼状图、散点图等都是一个图形对象,多个图形对象构成ChCharts集合。

ChartSpace对象就包含一个ChCharts集合,这些ChChart之间互相叠加,比如簇状条形图就是多个单柱状图的叠加,圆环套圆环图就是两个单圆环图的叠加。

通过ChCharts.Add()方法增加一个图形。

ChSeries对象、ChSeriesCollection集合、ChSeriesCollection.Add()方法:帮助中说:一个ChSeries对象表示图中的一个系列。

实际上,你可以这样理解,当图形容器中包含多个图形时,一个ChSeries对象就表示其中的一个图形,可以通过ChSeriesCollection集合,可以使用数组的方式访问各个ChSeries,比如ChSeriesCollection[0]表示第一个图形。

使用ChSeriesCollection.Add()方法在当前容器中新增一个图形。

Point属性和Points集合:一个Point代表图形中的一个部分,比如柱状图的一条柱,饼状图的一个扇区等。

ChChart对象提供Points集合,可以使用数组的形式访问各个Point,比如Points[0]表示第1个部分。

Interior属性:代表一个形状的部空间。

比如,ChartSpace对象的Interior属性代表图形容器、图形外的空间,一个扇区Interior属性表示该扇区的部空间。

该属性在设置图形各个部分的颜色时起到重要作用。

窃以为,以上对象和属性的理解、使用非常关键,顺着它们找下去,标题、图例、数据标签、字体等重要的特性都能顺利找到。

OWC简介及其属性(转)2008-10-21 17:06Figure 3 Office Web Components (version 10)组件描述Figure 4 PivotTable and Chart-related ObjectsFigure 5 PivotTable Component's Programmatic InterfaceFigure 6 Chart Component's Programmatic InterfaceFigure 8 Connecting to an OLAP Data Sourcefunction initializePivotTable(strDataMember) {// This function calls the InitializePivotTableXML() Web// methodvar iCallID =.svcOLAP.callService (onInitializePivotTableResult,'InitializePivotTableXML',strDataMember);}function onInitializePivotTableResult(result) {// This function handles the InitializePivotTableXML()// Web method resulttext = result.value; // result string// Evaluate return resultif (!result.error) {// Assign the XML to the PivotList XMLData valuefrm1.PivotTable1.XMLData = text;}else {alert("Unhandled error - " + result.errorDetail.code +" " + result.errorDetail.string);}}Figure 9 Generate XMLData for a PivotTable Control<WebMethod()> Public Function InitializePivotTableXML(ByVal _ strDataMember As String) As StringDim m_XML As StringDim strOLAPConn As String = _ConfigurationSettings.AppSettings("OLAPConnectionString")TryDim objPT As PivotTableClass = New PivotTableClassobjPT.ConnectionString = strOLAPConnobjPT.DataMember = strDataMemberm_XML = objPT.XMLDataobjPT = NothingCatch err As Exceptionm_XML = "<err>" & err.Source & " - " & err.Message & _"</err>"FinallyEnd TryReturn (m_XML)End FunctionFigure 10 LoadCustomPivotTableReport Web Method<WebMethod()> Public Function LoadCustomPivotTableReport(ByVal _ strCity1 As String, ByVal strCity2 As String) As StringDim m_XML As StringDim strOLAPConn As String = _ConfigurationSettings.AppSettings("OLAPConnectionString")Dim objPT As PivotTableClass = New PivotTableClassDim objPTView As PivotViewDim fldCity, fldName, fldProdFamily As PivotFieldDim fSetCustomers, fSetProduct As PivotFieldSetTryobjPT.ConnectionString = strOLAPConnobjPT.DataMember = "Sales"objPT.AllowFiltering = FalseobjPTView = objPT.ActiveViewobjPTView.TitleBar.Caption = "City Comparison of DrinkSales"' Define the column elementsobjPTView.ColumnAxis.InsertFieldSet(objPTView.FieldSets("Time")) objPTView.ColumnAxis.FieldSets("Time").Fields("Year").Expanded = True ' Define the row elementsfSetCustomers = objPTView.FieldSets("Customers")objPTView.RowAxis.InsertFieldSet(fSetCustomers)fSetCustomers.Fields("Country").IsIncluded = FalsefSetCustomers.Fields("State Province").IsIncluded = False fSetCustomers.Fields("Name").IsIncluded = False' Define the members of the row elementsfldCity = fSetCustomers.Fields("City")fldCity.IncludedMembers = New Object() {strCity1, strCity2}' Exclude all other field row members in the fieldsetfSetProduct = objPTView.FieldSets("Product")objPTView.RowAxis.InsertFieldSet(fSetProduct)fSetProduct.Fields("Product Department").IsIncluded = False fSetProduct.Fields("Product Category").IsIncluded = False fSetProduct.Fields("Product Subcategory").IsIncluded =FalsefSetProduct.Fields("Brand Name").IsIncluded = False fSetProduct.Fields("Product Name").IsIncluded = False fldProdFamily = fSetProduct.Fields("Product Family") fldProdFamily.IncludedMembers = "Drink"' Define the measuresobjPTView.DataAxis.InsertTotal(objPTView.Totals("Store Sales")) objPTView.DataAxis.Totals("Store Sales").NumberFormat = _ "Currency"' Return the XML data to the client side scriptm_XML = objPT.XMLDataobjPT = NothingCatch err As Exceptionm_XML = "<err>" & err.Source & " - " & err.Message & "</err>" FinallyEnd TryReturn (m_XML)End FunctionFigure 11 Load the XMLData for a Custom Reportfunction LoadSavedReport() {// Purpose: Call Web Service method to load the saved// reportvar iCallID =.svcOLAP.callService(onLoadSavedReportResult,'LoadSavedReport', 'OLAPReport1.xml');}function onLoadSavedReportResult(result) {// Purpose: This function handles the// wsOLAP.onLoadSavedReportResult() Web Service resultvar text = result.value; // result string// Evaluate return resultif (!result.error) {// Assign the XML to the PivotList XMLData valuefrm1.PivotTable1.XMLData = text;}}Figure 12 JavaScript and VBScript Event Handler<script language="javascript" event="Query" for="PivotTable1"> {var sLog = document.Form1.Text1.value + "";document.Form1.Text1.value = "Query Event Fired. " + sLog;}</script><script language="vbscript">Sub PivotTable1_CommandExecute(Command, Succeeded)Dim ptConstantsSet ptConstants = document.Form1.PivotTable1.Constants' Check to see if the PivotTable list has been' refreshed.If Command = ptConstants.plCommandRefresh Then' Write the current data and time to the text box.document.Form1.Text1.value = vbCrLf & _"PivotTable Last Refreshed on " & Date & " at " _& Time & vbCrLf & document.Form1.Text1.valueEnd IfEnd Sub</script>Figure 13 Creating Custom Groups<WebMethod()> Public Function ApplyCustomGrouping(ByVal _ strReportXMLData As String) As StringDim m_xml As StringDim objPT As PivotTableClass = New PivotTableClassDim objPTView As PivotViewDim fsTime As PivotFieldSetDim fsHalfYear As PivotFieldTryobjPT.XMLData = strReportXMLDataobjPTView = objPT.ActiveView' Set a variable to the Time field set.fsTime = objPTView.FieldSets("Time")' Add a custom group field named "Group1" to the Time field' set.fsHalfYear = fsTime.AddCustomGroupField("CustomGroup1", _ "CustomGroup1", "Quarter")' Add a custom field set member. This member includes all' "Q1" and "Q2" members under 1997.fsHalfYear.AddCustomGroupMember _(fsTime.Member.ChildMembers("1997").Name, _New Object() {"Q1", "Q2"}, "1stHalf")' Add another custom fieldset member to include all "Q3"' and "Q4" members under 1997.fsHalfYear.AddCustomGroupMember _(fsTime.Member.ChildMembers("1997").Name, _New Object() {"Q3", "Q4"}, "2ndHalf")' Collapse the fieldset at the custom member levelfsHalfYear.Expanded = Falsem_xml = objPT.XMLDataobjPT = NothingCatch err As Exceptionm_xml = "<err>" & err.Source & " - " & err.Message & _"</err>"FinallyEnd TryReturn (m_xml)End Function1.1. Excel试验:A. Excel嵌入网页的方法。