VB.Net快速打印Excel模板

我正在使用VB.Net ERP应用程序,用于修改客户,产品,库存详细信息和计费产品。

要打印帐单,我使用了预定义的Excel模板。 点击打印button后,只需打开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,而应用程序启动本身,所以,点击打印button,它只会传递的价值,并在几秒钟内打印Excel。 我知道在全局创buildvariables,但是我不知道如何在全局编写方法,并使用全局的Excel对象来传递值。

更新:我已经创build了模块,并在该模块方法中具有Excel的开放代码。 我只是从应用程序启动事件调用方法。

它会好吗?