飞狐.net

人的每一步行动,都在书写自己的历史
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

在.net中使用委托实现输出Excel时显示进度条

Posted on 2006-12-30 18:23  飞狐时代  阅读(2280)  评论(0)    收藏  举报
  本人在一个项目中,由于考虑界面友好,在输出Excel时,需要同时显示进度条,以反映输出的进度。

 首先,有一个主页面的后台文件frmMain.vb,里面有一个方法,是响应点击输出按钮的。如下:

   Private Sub mnuExport_Click()Sub mnuExport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuExport.Click
        Dim fex As New ExportToExcel

        Dim sfd As SaveFileDialog = New SaveFileDialog

        stpStatus.Text = "Export to"
        Try
            sfd.Title = "Save As"
            sfd.RestoreDirectory = True
            sfd.Filter = "Excel files (*.xls)|*.xls"
            sfd.FilterIndex = 1
            sfd.FileName = "WIPCount" & Now.Year & Now.Month & Now.Day & Now.Hour & Now.Minute & Now.Second

            Dim title As ArrayList = New ArrayList
            title.Add("ID")
            title.Add("Sheet Number")
            title.Add("Line No")
            title.Add("Branch Plant")
            title.Add("Item Number")
            title.Add("Stock Take Qty")
            title.Add("Location")
            title.Add("WO No")
            title.Add("Production Line")
            title.Add("Remark")
            title.Add("Created Date")
            title.Add("Created User")

            If sfd.ShowDialog = DialogResult.OK Then
                'Me.Cursor = Cursors.WaitCursor

                sfd.Dispose()

                fex.mydv = mydv
                fex.myfile = sfd.FileName
                fex.mytitle = title

                ‘//在本类中响应委托
                AddHandler fex.evVisibleProgress, AddressOf Me.VisibleProgress
                AddHandler fex.evShowProgress, AddressOf Me.ShowProgressbar
‘//启动线程进得导入                
Dim mThread As Thread
                mThread = New Thread(AddressOf fex.DataViewtToExcel)
                mThread.Start()

                'fex.DataViewtToExcel(mydv, sfd.FileName, title)
                'MessageBox.Show("Export to Excel file '" & sfd.FileName & "' successfully!", "Export", MessageBoxButtons.OK, MessageBoxIcon.Information)
            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message, "ExportData", MessageBoxButtons.OK, MessageBoxIcon.Error)
            fer.ErrorTxt(ex.Message, "Export Data")
            stpStatus.Text = "Export occur error!"
        End Try

    End Sub

‘//用来显示进度条进度
Private Sub ShowProgressbar()Sub ShowProgressbar(ByVal Position As Integer, ByVal Count As Integer, ByVal f As frmProgress)
        f.pgbExport.Maximum = Count
        f.pgbExport.Minimum = 0
        f.pgbExport.Value = Position

        Dim min As Integer
        Dim numerator, denominator, completed As Double

        min = f.pgbExport.Minimum
        numerator = f.pgbExport.Value - min
        denominator = f.pgbExport.Maximum - min
        If denominator <> 0 Then
            completed = numerator / denominator * 100
        Else
            completed = 0
        End If
        stpStatus.Text = "Exporting:" & Math.Round(completed).ToString & "%"
    End Sub

‘//用来确定是否显示进度条
    Private Sub VisibleProgress()Sub VisibleProgress(ByVal blnShow As Boolean, ByVal f As frmProgress)
        If blnShow Then
            If f.WindowState <> FormWindowState.Minimized Then
                f.Show()
                f.Refresh()
            End If
        Else
            f.Hide()
            f.Close()
            'f.Dispose()
            stpStatus.Text = "Export to Excel file is complete!"
           'Me.Cursor = Cursors.Default
        End If
End Sub

‘//输出Excel的类文件
Imports System
Imports System.Data
Imports System.IO
Imports Excel
Imports System.Diagnostics
Imports System.Data.SqlClient
Imports WIPCount.Component

Public Class ExportToExcelClass ExportToExcel
    Private prePID As ArrayList = New ArrayList
    Private lastPID As ArrayList = New ArrayList

    Private mdv As DataView
    Private mfile As String
    Private mtitle As ArrayList

   '//定义委托
    Delegate Sub ShowProgress()Sub ShowProgress(ByVal Position As Integer, ByVal Count As Integer, ByVal fp As frmProgress)
    Delegate Sub VisibleProgress()Sub VisibleProgress(ByVal show As Boolean, ByVal fp As frmProgress)
    '//定义事件
    Public Event evShowProgress As ShowProgress
    Public Event evVisibleProgress As VisibleProgress

    Public Property mydv()Property mydv() As DataView
        Get
            Return mdv
        End Get
        Set(ByVal Value As DataView)
            mdv = Value
        End Set
    End Property

    Public Property myfile()Property myfile() As String
        Get
            Return mfile
        End Get
        Set(ByVal Value As String)
            mfile = Value
        End Set
    End Property

    Public Property mytitle()Property mytitle() As ArrayList
        Get
            Return mtitle
        End Get
        Set(ByVal Value As ArrayList)
            mtitle = Value
        End Set
    End Property


    Public Sub DataViewtToExcel()Sub DataViewtToExcel()
        Dim fp As frmProgress = New frmProgress
‘//此处显示进度条窗体
        RaiseEvent evVisibleProgress(True, fp)

        getProcessID(prePID)

        Dim xlApp As New Excel.Application
        Dim xlBook As Excel.Workbook
        Dim xlSheet As Excel.Worksheet
        Dim xbookname As String = ""
        Dim rowIndex, colIndex As Integer
        Dim myTable As System.Data.DataTable
        Dim isIn As Boolean = False

        Try
            rowIndex = 1
            colIndex = 0

            xbookname = mfile
            If File.Exists(xbookname) Then
                xlBook = xlApp.Workbooks.Add(xbookname)
            Else
                xlBook = xlApp.Workbooks.Add()
            End If

            xlSheet = xlBook.Worksheets("sheet1")

            myTable = mdv.Table

            Dim intRow, intCol As Integer

            For intRow = 0 To myTable.Rows.Count - 1
                rowIndex = rowIndex + 1
                colIndex = 0

                For intCol = 1 To myTable.Columns.Count - 1
                    colIndex = colIndex + 1
                    'xlApp.Cells(rowIndex, colIndex) = Row(myTable.Columns(intcol).ColumnName)
                    xlApp.Cells(rowIndex, colIndex) = myTable.Rows(intRow).Item(intCol).ToString
                Next
‘//此处刷新进度条
                RaiseEvent evShowProgress(intRow + 1, myTable.Rows.Count, fp)
                isIn = True
            Next

            If isIn = True Then
                Dim t As Integer
                colIndex = 0
                For t = 1 To mtitle.Count - 1 
                    colIndex = colIndex + 1
                    xlApp.Cells(1, colIndex) = mtitle(t)
                Next

                xlBook.Saved = True
                xlApp.UserControl = False
                xlBook.SaveAs(xbookname)
                KillProcess("EXCEL")
            Else
                xlBook.Saved = False
                xlApp.UserControl = False
                KillProcess("EXCEL")
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, "DataViewtoExcel", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
           ‘//完成后关闭进度条窗体
            RaiseEvent evVisibleProgress(False, fp)
        End Try
    End Sub

    End Class

  注:文中frmProgress是进度条的窗体页面,类名为frmProgress.

同时,在导入Excel时,我使用了多线程技术,这样导出Excel时,用户还可以做其他操作,导出完成后,给出了提示信息,用户就知道导入完成了。代码如下:

Dim mThread As Thread

                mThread = New Thread(AddressOf fex.DataViewtToExcel)

                mThread.Start()

以上是启动线程执行导入动作,如果直接使用fex.DataViewtToExcel访问输出Excel的类,是达不到这种效果的。所以本文是使用多线程实现导出Excel,同时使用委托显示进度条。

飞狐.net·书写程序员的历史·创建于2006年12月
Copyright 2005 Silver fox© 飞狐.net All Rights Reserved.