当打印额外的白纸是通过发送! 为什么?

在VB.net中得到这个algorithm,它操作和打印一个Excel文档,但每次我打印我得到一张额外的纸张之前,我想打印的文件! Excel表格上没有额外的页面或类似的东西! 任何人都可以发现为什么在这个代码或有人知道为什么吗? 谢谢

Sub Cmd_PrintClick(sender As Object, e As EventArgs) 'print 1 page printDocument.PrinterSettings.Copies = 1 'print document (see Sub PrintDocumentPrintPage below ...) printDocument.Print End Sub Sub PrintDocumentPrintPage(sender As Object, e As System.Drawing.Printing.PrintPageEventArgs) Dim oApp As New Excel.Application Dim oWB As Excel.Workbook = oApp.Workbooks.Add() Dim oWS As Excel.Worksheet = CType(oWB.Worksheets(1), Excel.Worksheet) Dim oRng1 As Excel.Range Dim search As String = "" SQLcommand = SQLconnect.CreateCommand 'Create SQL statement SQLcommand.CommandText = "SELECT * FROM Bill_Record WHERE Paid = 'F'" 'Extract data SQLreader = SQLcommand.ExecuteReader() While SQLreader.Read() search = "" search = SQLreader("Pupil_ID") oWB = oApp.Workbooks.Open("F:\Kids Club Database\Kids Club Database v2\Backup\ContactsDatabase\bin\Debug\Bill.xlsx") oWS = oWB.Worksheets("Sheet1") oRng1 = oWS.Range("F34") oRng1.Value = Microsoft.VisualBasic.Left(SQLreader("Payment_Due_Date"),10) oRng1 = oWS.Range("A19") oRng1.Value = Microsoft.VisualBasic.Left(SQLreader("Bill_Date"),10) oRng1 = oWS.Range("A13") oRng1.Value = SQLreader("Term") oRng1 = oWS.Range("C47") oRng1.Value = SQLreader("Term") oRng1 = oWS.Range("H28") oRng1.Value = SQLreader("Total_Term_Cost") oRng1 = oWS.Range("C48") oRng1.Value = SQLreader("Total_Term_Cost") oRng1 = oWS.Range("F22") oRng1.Value = SQLreader("Total_Sessions_Monday") oRng1 = oWS.Range("H22") oRng1.Value = SQLreader("Total_Monday_Cost") oRng1 = oWS.Range("F23") oRng1.Value = SQLreader("Total_Sessions_Tuesday") oRng1 = oWS.Range("H23") oRng1.Value = SQLreader("Total_Tuesday_Cost") oRng1 = oWS.Range("F24") oRng1.Value = SQLreader("Total_Sessions_Wednesday") oRng1 = oWS.Range("H24") oRng1.Value = SQLreader("Total_Wednesday_Cost") oRng1 = oWS.Range("F25") oRng1.Value = SQLreader("Total_Sessions_Thursday") oRng1 = oWS.Range("H25") oRng1.Value = SQLreader("Total_Thursday_Cost") oRng1 = oWS.Range("F26") oRng1.Value = SQLreader("Total_Sessions_Friday") oRng1 = oWS.Range("H26") oRng1.Value = SQLreader("Total_Friday_Cost") 'Clear SQL command buffer SQLcommand.Dispose() SQLcommand = SQLconnect.CreateCommand 'Create SQL statement SQLcommand.CommandText = "SELECT Pupil_Name, Pupil_Surname, Form_ID FROM Pupil WHERE Pupil_ID = '" & search &"'" 'Extract data SQLreader = SQLcommand.ExecuteReader() oRng1 = oWS.Range("A16") oRng1.Value = SQLreader("Pupil_Name") & " " & SQLreader("Pupil_Surname") oRng1 = oWS.Range("C46") oRng1.Value = SQLreader("Pupil_Name") & " " & SQLreader("Pupil_Surname") oRng1 = oWS.Range("A17") oRng1.Value = SQLreader("Form_ID") 'Clear SQL command buffer SQLcommand.Dispose() SQLcommand = SQLconnect.CreateCommand 'Create SQL statement SQLcommand.CommandText = "SELECT * FROM Price" 'Extract data SQLreader = SQLcommand.ExecuteReader() oRng1 = oWS.Range("D22") oRng1.Value = SQLreader("Price_Monday") oRng1 = oWS.Range("D23") oRng1.Value = SQLreader("Price_Tuesday") oRng1 = oWS.Range("D24") oRng1.Value = SQLreader("Price_Wednesday") oRng1 = oWS.Range("D25") oRng1.Value = SQLreader("Price_Thursday") oRng1 = oWS.Range("D26") oRng1.Value = SQLreader("Price_Friday") 'Clear SQL command buffer SQLcommand.Dispose() oRng1 = Nothing ' <-- Don't forget! oWB.PrintOut() oWB.Close() oWB = Nothing oWS = Nothing oApp.Quit() oApp = Nothing End While End Sub 

尝试改变:

 oWB.PrintOut() 

至:

 oWB.PrintOut(From:=1, [To]:=1, Copies:=1, Collate:=False) 

编辑

此外,一定要设置所需的PageSetup (表单):

 With oWS.PageSetup .Zoom = False .PaperSize = XlPaperSize.xlPaperA4 .Orientation = XlPageOrientation.xlPortrait .FitToPagesTall = 1 .FitToPagesWide = 1 End With 

编辑2

那么为什么要多一个空白页? 这很简单。 您已经将您的Excel Interop代码放在PrintDocumentPrintPageEvent中。 所以额外的页面正在由PrintDocument打印。

 Sub PrintDocumentPrintPage(sender As Object, e As System.Drawing.Printing.PrintPageEventArgs) e.Cancel = True '< Do not print 'YOUR CODE... End Sub 

或者把所有东西都移到一个单独的sub中,并在需要时调

 Private Sub PrintXLS() 'YOUR CODE... End Sub 

Dim oWS As Excel.Worksheet = CType(oWB.Worksheets(1),Excel.Worksheet)

我认为应该是:

Dim oWS As Excel.Worksheet = CType(oWB.Worksheets(0),Excel.Worksheet)

编辑1:

怎么样:

Dim oWS As Excel.Worksheet = oWB.Worksheets.Add(New Excel.Worksheet)

我认为发生了什么事情就是在工作表位置1而不是工作表位置0添加工作表,所以第一个工作表(空白的)先打印,然后打印您正在处理的工作表。

编辑2:

 oWB = oApp.Workbooks.Open("F:\Kids Club Database\Kids Club Database v2\Backup\ContactsDatabase\bin\Debug\Bill.xlsx") oWS = oWB.Worksheets("Sheet1") oRng1 = oWS.Range("F34") ' Put a break point on this line and check the oWB.Worksheets collection and see how many worksheets there are in the collection and make sure the first one is named "Sheet 1"