www.pudn.com > chapter11-4.rar > Form1.vb


Imports System.Data.SqlClient 
Public Class Form1 
    Inherits System.Windows.Forms.Form 
 
#Region " Windows 窗体设计器生成的代码 " 
 
    Public Sub New() 
        MyBase.New() 
 
        '该调用是 Windows 窗体设计器所必需的。 
        InitializeComponent() 
 
        '在 InitializeComponent() 调用之后添加任何初始化 
 
    End Sub 
 
    '窗体重写处置以清理组件列表。 
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) 
        If disposing Then 
            If Not (components Is Nothing) Then 
                components.Dispose() 
            End If 
        End If 
        MyBase.Dispose(disposing) 
    End Sub 
 
    'Windows 窗体设计器所必需的 
    Private components As System.ComponentModel.IContainer 
 
    '注意:以下过程是 Windows 窗体设计器所必需的 
    '可以使用 Windows 窗体设计器修改此过程。 
    '不要使用代码编辑器修改它。 
    Friend WithEvents Button1 As System.Windows.Forms.Button 
     Private Sub InitializeComponent() 
        Me.Button1 = New System.Windows.Forms.Button() 
        Me.SuspendLayout() 
        ' 
        'Button1 
        ' 
        Me.Button1.Location = New System.Drawing.Point(152, 184) 
        Me.Button1.Name = "Button1" 
        Me.Button1.Size = New System.Drawing.Size(104, 40) 
        Me.Button1.TabIndex = 0 
        Me.Button1.Text = "直方图" 
        ' 
        'Form1 
        ' 
        Me.AutoScaleBaseSize = New System.Drawing.Size(8, 18) 
        Me.ClientSize = New System.Drawing.Size(456, 260) 
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Button1}) 
        Me.Name = "Form1" 
        Me.Text = "应用Excel制作直方图" 
        Me.ResumeLayout(False) 
 
    End Sub 
 
#End Region 
    Dim con As New SqlConnection("server=localhost;database=northwind;" _ 
    & "integrated security=true") 
    Dim adapter As New SqlDataAdapter("select * from employeeamount", con) 
    Dim myset As New DataSet() 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 
        adapter.Fill(myset, "emp") 
        Dim myexcel As New Excel.Application() 
        myexcel.Visible = True 
        myexcel.Workbooks.Add() 
        myexcel.Worksheets("sheet1").Activate() 
        myexcel.Cells(1, 1).value = "雇员销售业绩直方图" 
        Dim col = 0 
        Dim row = 0 
        For col = 0 To 1 
            For row = 0 To myset.Tables("emp").Rows.Count - 1 
                myexcel.Cells(row + 2, col + 1).value = myset.Tables("emp").Rows(row)(col) 
            Next 
        Next 
        '做直方图 
        myexcel.Range("a2:b" & row + 1).Select() 
        myexcel.Charts.Add() 
        myexcel.ActiveChart.ChartType = 51 '从51开始,51表示平面直方图 
        myexcel.ActiveChart.HasTitle = True 
        myexcel.ActiveChart.ChartTitle.Characters.Text = "销售业绩表" 
        '定义Y坐标轴 
        myexcel.ActiveChart.Axes(Excel.XlAxisType.xlValue, Excel.XlAxisGroup.xlPrimary).HasTitle = True 
        myexcel.ActiveChart.Axes(Excel.XlAxisType.xlValue, Excel.XlAxisGroup.xlPrimary).AxisTitle.Characters.Text = "订单金额" 
        '定义X坐标轴 
        myexcel.ActiveChart.Axes(Excel.XlAxisType.xlCategory, Excel.XlAxisGroup.xlPrimary).HasTitle = True 
        myexcel.ActiveChart.Axes(Excel.XlAxisType.xlCategory, Excel.XlAxisGroup.xlPrimary).AxisTitle.Characters.Text = "雇员号" 
    End Sub 
End Class