VB.Net快速使用Excel模板打印

时间:2017-04-19 05:08:03

标签: excel vb.net winforms

我正在研究VB.Net ERP应用程序,该应用程序用于修改客户,产品,库存详情和计费产品。

要打印帐单,我使用了预定义的Excel模板。单击“打印”按钮后,它将打开excel并使用Excel对象传递值,之后它将从应用程序背景中打印活动工作表,使用时不知道该进程。我在打印时遇到速度慢,完成整个打印操作大约需要1到2分钟。我使用下面的代码:

Private Function WriteDetailsInfileDOS(BillNumber As String, BillType As String, PrintCopy As Integer) As Boolean
        'Define your Excel Objects
        Dim xlApp As New Excel.Application
        Dim xlWorkBook As Excel.Workbook
        Dim xlWorkSheet As Excel.Worksheet

        Try
            Select Case BillType
                'Opens Source Workbook. Change path and filename as applicable
                Case "KAS"
                    xlWorkBook = xlApp.Workbooks.Open(AppDomain.CurrentDomain.BaseDirectory.Replace("\bin\Debug", "") + "Templates\S_Invoice_Template.xlsx")
                    'Select a worksheet
                    xlWorkSheet = xlWorkBook.Sheets("S_Invoice_Template")
                    Exit Select
                Case "SVC"
                    xlWorkBook = xlApp.Workbooks.Open(AppDomain.CurrentDomain.BaseDirectory.Replace("\bin\Debug", "") + "Templates\V_Invoice_Template.xlsx")
                    'Select a worksheet
                    xlWorkSheet = xlWorkBook.Sheets("V_Invoice_Template")
                    Exit Select
                Case "CHERAN"
                    xlWorkBook = xlApp.Workbooks.Open(AppDomain.CurrentDomain.BaseDirectory.Replace("\bin\Debug", "") + "Templates\C_Invoice_Template.xlsx")
                    'Select a worksheet
                    xlWorkSheet = xlWorkBook.Sheets("C_Invoice_Template")
                    Exit Select
                Case "ESTIMATE"
                    xlWorkBook = xlApp.Workbooks.Open(AppDomain.CurrentDomain.BaseDirectory.Replace("\bin\Debug", "") + "Templates\ESTIMATE_Template.xlsx")
                    'Select a worksheet
                    xlWorkSheet = xlWorkBook.Sheets("ESTIMATE_Template")
                    Exit Select
                Case Else
                    Return False
            End Select

            'Display Excel
            xlApp.Visible = False
            xlApp.AlertBeforeOverwriting = False

            xlWorkSheet.Cells(6, 9).value = BillNumber
            xlWorkSheet.Cells(8, 7).value = lblC_Code.Text

            xlWorkSheet.Cells(11, 4).value = txtCName.Text
            xlWorkSheet.Cells(12, 4).value = txtCAddress.Text
            xlWorkSheet.Cells(13, 4).value = txtCCity.Text
            xlWorkSheet.Cells(14, 4).value = txtCPincode.Text
            xlWorkSheet.Cells(15, 4).value = txtCPhone.Text
            xlWorkSheet.Cells(13, 7).value = txtCTIN.Text

            xlWorkSheet.Cells(10, 9).value = DateTime.Now.ToString("dd-MMM-yyyy")
            xlWorkSheet.Cells(11, 9).value = DateTime.Today.DayOfWeek.ToString()
            xlWorkSheet.Cells(12, 9).value = UserName.ToString()
            xlWorkSheet.Cells(14, 9).value = txtTotal.Text
            xlWorkSheet.Cells(34, 8).value = txtTotal.Text
            Dim RowCount As Integer = gvBill.RowCount
            Dim S As Integer = 0
            Dim BillDescCell As Integer = 17

            For Each Row As DataGridViewRow In Me.gvBill.Rows
                If BillDescCell = 33 Then
                    Exit For
                End If
                S = S + 1
                If Row.Cells(Me.Product.Index).Value <> "" Then
                    xlWorkSheet.Cells(BillDescCell, 3).value = S.ToString()
                    xlWorkSheet.Cells(BillDescCell, 4).value = Row.Cells(Me.Product.Index).Value.ToString
                    xlWorkSheet.Cells(BillDescCell, 6).value = Row.Cells(Me.Qty.Index).Value.ToString
                    xlWorkSheet.Cells(BillDescCell, 7).value = Row.Cells(Me.Unit.Index).Value.ToString
                    xlWorkSheet.Cells(BillDescCell, 8).value = Row.Cells(Me.Rate.Index).Value.ToString
                End If
                BillDescCell = BillDescCell + 1
            Next

            ' Printing the Excel Workbook
            xlWorkSheet.PrintOut(From:=1, To:=1, Copies:=PrintCopy, Collate:=True)
            Return True
        Catch ex As Exception
            ExceptionLog(Me.GetType.Name.ToString, "WriteDetailsInfileDOS", ex.ToString().ToString, DateTime.Now.ToString("dd-MMM-yyyy"))
            Return False
        Finally
            ReleaseComObject(xlWorkSheet)
            ReleaseComObject(xlWorkBook)
            ReleaseComObject(xlApp)
        End Try
    End Function

我认为这种缓慢是因为在每次打印点击时打开了excel。我认为,我们可以打开Excel,同时应用程序启动本身,这样,点击打印按钮,它只会传递值并在几秒钟内打印excel。我知道全局创建变量但我不知道如何全局编写方法并全局使用excel对象来传递值。

更新: 我已经创建了模块并在该模块方法中使用了excel开放代码。我刚从应用程序启动事件中调用了该方法。

会不会好?

0 个答案:

没有答案