To invoke Excel2000 in a VB component to implement a GIF pie chart

Source: Internet
Author: User
Excel| pie chart When I first used Excel, I was overwhelmed with Excel's chart features, and the graphs were pretty good.
。 Later, I tried to call Excel in VB to support the VBA function, found that the function is really powerful, is very cumbersome. Later, we consider using VB to pack a layer outside of Excel, write objects, remove the characteristics we do not need. It is more convenient to use it, the so-called:P once and for all.

Here, I will like you to introduce a VB-written pie chart components, you only need to give it a few simple parameters, you can generate a GIF format picture to you. The invocation example is as follows:

Dim obj
Set obj = CreateObject ("Chinaaspchart.pie")
Obj. AddValue "Male", 150
Obj. AddValue "female", 45
Obj. AddValue "Don't Know", 15
Obj. Chartname = "Sex scale graph"
Obj. FileName = "D:\123.gif"
Obj. Savechart


In addition to the VB can be called, this code can also be called in the ASP.

Here's how to write our components.

1.New project, select the ActiveX DLL, select Project1 on the Project Explorer panel, and modify its name to Chinaaspchart on the properties panel. Also change the inside class modules to pie.

2. Save the project, store project as Chinaaspchart.vbp, and save the Class1.cls as a pie.cls.

3. Menu project, select menu item references, and then please bring Microsoft Active Server Pages ojbect Library, Microsoft Excel 9.0 Object Library, COM + The Services Type Library is selected.
Note: There is no COM + Service type library on Nt4/win98, you should select the Microsoft Transaction Server type library

4. Edit Pie.cls, code as follows:


Note:-------------------------------------------------------------------------------
Dim XL
Dim M_chartname
Dim M_chartdata ()
Dim M_charttype
Dim M_filename
Public errmsg
Public Founderr
Dim icount

Type m_value
Label as String
Value as Double
End Type

Dim TValue as M_value

Public Property Let ChartType (ChartType)
M_charttype = ChartType
End Property

Public Property Get ChartType ()
ChartType = M_charttype
End Property

Public Property Let Chartname (Chartname)
M_chartname = Chartname
End Property

Public Property Get Chartname ()
Chartname = M_chartname
End Property

Public Property Let FileName (fname)
M_filename = fname
End Property
Public Property Get FileName ()
FileName = M_filename
End Property

Public Sub addvalue (label, value)
icount = icount + 1
ReDim Preserve M_chartdata (icount)
Tvalue.label = Label
Tvalue.value = value
M_chartdata (icount) = TValue
End Sub

Public Sub Savechart ()
On Error Resume Next
Dim Isheet
Dim I
Set XL = New Excel.Application
Xl. Application.Workbooks.Add
Xl. Workbooks (1). Worksheets ("Sheet1"). Activate
If err.number <> 0 Then
Founderr = True
ErrMsg = Err.Description
Err.Clear
Else
Xl. Workbooks (1). Worksheets ("Sheet1"). Cells ("2,1"). Value = M_chartname
For i = 1 to icount
Xl. Worksheets ("Sheet1"). Cells (1, i + 1). Value = M_chartdata (i). Label
Xl. Worksheets ("Sheet1"). Cells (2, i + 1). Value = M_chartdata (i). Value
Next
Xl. Charts.add
Xl. Activechart.charttype = M_charttype
Xl. Activechart.setsourcedata XL. Sheets ("Sheet1"). Range ("A1:" & Chr (icount Mod) + ASC ("A") & "2"), 1
Xl. Activechart.location 2, "Sheet1"
With XL. ActiveChart
. HasTitle = True
. ChartTitle.Characters.Text = M_chartname
End With
Xl. Activechart.applydatalabels 2, False, _
True, False
With XL. Selection.border
. Weight = 2
. LineStyle = 0
End With

Xl. ActiveChart.PlotArea.Select
With XL. Selection.border
. Weight = Xlhairline
. LineStyle = Xlnone
End With
Xl. Selection.Interior.ColorIndex = Xlnone

Xl. Activewindow.visible = False

Xl. DisplayAlerts = False

Xl. Activechart.export m_filename, filtername:= "GIF"
Xl. Workbooks.close
If err.number <> 0 Then
Founderr = True
ErrMsg = ErrMsg
Err.Clear
End If
End If
Set xl = Nothing
End Sub

Private Sub Class_Initialize ()
icount = 0
Founderr = False
ErrMsg = ""
M_charttype =-4102 Note: xl3DPie
Note: 54 Note: Column chart
End Sub

Note:-------------------------------------------------------------------------------

What if the bar chart is implemented?
In fact, the previous code has implemented the function of the histogram, but the default is the pie chart feature. The calling code is changed as follows:

Dim obj
Set obj = createobj



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.