Sub SetXAxisHeadings(ByVal oRs As ADOdb.Recordset, ByVal strValueCol As String) Dim iCount As Integer Dim iRow, iCol As Integer Dim nMaxRows As Integer Dim oFill As excel.ChartFillFormat '--- 检查参数是否合法 If (IsNull(oRs) Or IsNull(strValueCol) _ Or oExcelChart.SerieSCOllection.Count < 1) Then Err.Raise Number:=1001 + vbobjectError, _ Description:="Invalid recordset or column name" Exit Sub End If On Error GoTo hError '--- 单个数据系列中最大数据个数 nMaxRows = 25 '--- 设置初始值和位置 oRs.MoveFirst iRow = 0: iCol = 1 '--- 循环,将记录集中的数据写入工作表第一列 While (Not oRs.EOF And iRow < nMaxRows) iRow = iRow + 1 '--- 设置单元格的值 oExcelSheet.Cells(iRow, iCol) = CStr(oRs(strValueCol).Value) '--- 下一行 oRs.MoveNext Wend '--- 检查是否确实写入了数据 If (iRow > 0) Then '--- 将这些数据设置为X-轴标签 oExcelChart.SeriesCollection(1).XValues = _ oExcelSheet.Range(oExcelSheet.Cells(1, iCol), _ oExcelSheet.Cells(iRow, iCol)) End If Exit Sub hError: App.LogEvent Err.Description, vbLogEventTypeError Err.Raise Err.Number, Err.source, Err.Description End Sub
该方法的处理步骤与AddDataSeries()方法相似。标题也是作为记录集的一个列传入,其中的值被加入到工作表中保留的第一个列,然后程序使用Series对象的XValues对象和工作表对象的Range()方法将这些数据加入图表。Range()方法返回的是一个区域,该区域被赋给了Chart对象的XValues对象。
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/10752043/viewspace-988514/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/10752043/viewspace-988514/
本文详细介绍了SetXAxisHeadings函数的功能及其实现过程。该函数通过ADO记录集获取数据,并将其写入Excel工作表的第一列,再将这些数据设置为图表的X轴标签。适用于将数据库中的数据直接转化为图表的应用场景。

被折叠的 条评论
为什么被折叠?



